Search code examples
objective-ciosxcodecocos2d-iphoneccsprite

Getting absolute position of CCSprite in cocos2d


In my game, I have a CCSprite that orbits another CCSprite, much like an electron orbiting a nucleus. I have the electron as a child of the nucleus, to make animation much simpler. All I have to do is rotate the nucleus and the electron follows suite quite nicely.

However, my problem comes from wanting to have the orbit animation appear a little snazzier, by either adding something like a particle system trail, or a ribbon effect following the path of the electron. I cannot simply add a particle system to the electron itself, because the particles don't follow correctly, since they are being rotated by the nucleus as well. If I add the particle system to self, then they appear correctly, but not in the same position as the object they are supposed to be trailing.

My question is this:

Is there a way to get the scene position of an object, say the electron, as opposed to only having access to it's position relative to it's parent?

Thanks.


Solution

  • Yes there is!

    Each CCNode and its descendants has the ability to get a position relative to the scene:

    CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];

    This worldCoordinate will be relative to the scene as opposed to the parent node!

    Hope this helped! ^_^