Search code examples
iosanimationcocos2d-iphoneccsprite

(Cocos2D) Sprite not perfectly sitting on top of other sprite?


Something is very odd in my app. I must have looked over this line countless times and I swear I do not see anything wrong with it. Pretty much at the click of a button, this code would get executed and sprite A should sit on top of sprite B.

No I can't just do setPosition to the place since sprite A will be moving along with sprite B on top of it during an animation.

So what happens here, is that sprite A ends up being like 30 pixels too high over the top of sprite B. I do not know why this is. This is my code to do this:

spriteA.position = ccp(cgpoint.x , spriteB.position.y + spriteB.boundingBox.size.height/2 + spriteA.contentSize.height/2);

So what happens here is I am setting sprite A's X position to a CGPoint that doesn't relate to this question but I set my Y position to the current position spriteB is at, then I add half the height of spriteB to account for the anchor point issue, then lastly, I add half the height of spriteA so that it should sit perfectly on top of spriteB during the animation.

Is there any reason why this is not working as I want it to?

Thanks!


Solution

  • You use boundingBox for one sprite and contentSize for other:

    spriteB.boundingBox.size.height/2 + spriteA.contentSize.height/2
    

    Is your spriteA scaled down by any chance?