CCSprite positioning help

CCSprite positioning help
0.0 0


I have a CCSprite that gets placed on the screen. Lets call it Sprite A.

Later on I want to place Sprite B, on top of Sprite A.

So I am trying:

cocos2d::CCPoint location = cocos2d::CCPoint(SpriteA->getPosition().x,

SpriteB->setPosition(SpriteA.x, SpriteA.y));

 SpriteA ->addChild(SpriteB, 3);

Sprite B is getting added to the screen but not in the right place. It seems that where it is being added is somehow, maybe relative to something else.

Can anyone help me to do this better?


You are adding SpriteB as child of SpriteA, so of course the position is relative (to Sprite A).
You should add SpriteB as child of the same parent as SpriteA for the to share the system of coordinates, or transform the coordinates to the system used by SpriteA.


the default anchor point is ccp(0.5,0.5)


If you need or want SpriteB to be a child of SpriteA, the correct position would depend on the anchor points of both images. For example:

SpriteA->setAnchorPoint(ccp(0,0)); // the anchor point is in the bottom-left corner of SpriteA
SpriteB->setAnchorPoint(ccp(0,0)); // the same
        0   // because the position is relative to SpriteA's bottom-left corner, X position should be 0
        ,SpriteA->boundingBox().size.height // to get the Y position relative to the bottom-left corner of SpriteA, use SpriteA's height.

Hope that helps!


what actually worked was adding as a child first, then setting the position.


SpriteA ->addChild(SpriteB, 3);
SpriteB->setPosition(SpriteA.x, SpriteA.y));

Thanks everyone!