0
votes

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!

2

2 Answers

1
votes

if you want to set A top of B sprite you should take care of the z order of the sprite. if A have more z value A will be top of B when A and B have same position and anchor point.

You can set the z Value when you add the child Like this:

[self addChild:spriteA z:1 tag:<tagValue int>];
[self addChild:spriteB z:0 tag:<tagValue int>];
1
votes

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?