No rectangle showing!?

hi!

i’ve just started a new project and wanted to create a simple rectangle but it’s not showing, take a look:

Code: [code] auto rectNode = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-50, -50);
rectangle[1] = Vec2(50, -50);
rectangle[2] = Vec2(50, 50);
rectangle[3] = Vec2(-50, 50);

Color4F white(1, 1, 1, 1);
rectNode->drawPolygon(rectangle, 4, white, 1, white);
this->addChild(rectNode);[/code]

Outcome:

Any ideas, thanks!

Hey,

you forgot to set Position:

 visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();

auto rectNode = DrawNode::create();
	Vec2 rectangle[4];
	rectangle[0] = Vec2(-50, -50);
	rectangle[1] = Vec2(50, -50);
	rectangle[2] = Vec2(50, 50);
	rectangle[3] = Vec2(-50, 50);
	Color4F white(1, 1, 1, 1);
	rectNode->drawPolygon(rectangle, 4, white, 1, white);
	rectNode->setPosition(Vec2(visibleSize.width / 2, visibleSize.height /2));
	this->addChild(rectNode);

haha knew it was such a stupid thing but THANKS!<3
It worked. May the force be with you!

One more thing…Do you know how i can make the rectangle have a width that’s as long as the screen width?

Hey,

glad I could help.
try this:

auto halfWidth = visibleSize.width / 2;
rectangle[0] = Vec2(-halfWidth, -50);
rectangle[1] = Vec2(halfWidth, -50);
rectangle[2] = Vec2(halfWidth, 50);
rectangle[3] = Vec2(-halfWidth,50);

Thanks, you was right once again…But can’t i set its anchor point, i mean so that i can place the bottom of the rec at the bottom of the screen? Can’t get it to work using this rectNode->setAnchorPoint(Vec2(0.5, 0.0)); rectNode->setPosition(Vec2(visibleSize.width / 2 + origin.x, 0 ));
Any idea? With this, it’s just like that the anchor points is all 0.5? and i have changed it to 0.0

Thanks!

Hey,

this does the trick:

rectNode->setPosition(Vec2( origin.x + halfWidth, origin.y + 50));

It appears to me that AnchorPoint doesnt affect it at all which is confusing to me too.
Maybe someone else could clarify this.

Thank you!
Maybe @slackmoehrle can explain…
And @dpd93 can you help me with ONE more thing?

In case you say yes:

Would you create this directly from code as the rectangle or just import the image which takes more memory…

And anyway, how could I create this directly from code

Thanks!

Hey,

I am not very experienced in cocosd-x, just started to develop a game.
That said I can only offer limited help but I’m glad if I can help.
I would just take an image because I think cocos handles that very well so it won’t be a big issue.
If you want to save memory use a spritesheet.
Maybe I could help you better if you can tell me what exactly your app is and what you want to do.

Hah look the funny thing is that I’ve programmed four games and i’m not sure about this openGL…It’s strange but yea I think i’ll just import it in a spritesheet with all the other images…Again Thanks FOR ALL!<333333333333333333333333333333 And btw when you are done with your game I can promote it, I have contacts you know :wink: As a gift for your help, contact me for further info at vuvysoft@gmail.com

Yes, I do have an explanation about anchor point versus setPosition():

http://cocos2d-x.org/programmersguide/3/index.html#sprite-manipulation

anchor point is not the equivalent of setPosition()`.

Well I know that but neither of the following did make a change when I tested:

 rectNode->setAnchorPoint(Vec2(0.5, 0.0));
    rectNode->setPosition(Vec2(visibleSize.width / 2 + origin.x, 0 ));

 rectNode->setAnchorPoint(Vec2(0.5, 0.5));
    rectNode->setPosition(Vec2(visibleSize.width / 2 + origin.x, 0 ));

 rectNode->setAnchorPoint(Vec2(0.0, 0.0));
    rectNode->setPosition(Vec2(visibleSize.width / 2 + origin.x, 0 ));

And I use setAnchorPoint sometimes on my Sprites so I#m confused why it doesnt affect rectNode

Exactly. Why doesn’t it affect DrawNodes?

Ah, I see, so neither setAnchorPoint nor setPosition are having an effect on a DrawNode?

Yes, setPosition does but the anchorPoint stays at 0.5 no matter what…

Personally for something like that I’d try to draw it rather than use sprites.

I fill areas using code based on PRKit https://github.com/dmazzella/cocos2d-x-PRKit which allows you to fill shapes with textures; obviously in your case it looks like you just want to fill squares - but you can use the code from PRKit to see how to draw textures.

Drawing lines is reasonably trivial too.

And the advantage of drawing over sprites is the flexibility it gives you - especially with the lines joining your boxes.