0
votes

Porting my game from Cocos2d v2 to v3 I don't know when the sprites go out of screen.

In v2 my solution was:

-(void) update:(ccTime) delta
{
   // Should use a fixed size step based on the animation interval.
   int steps = 2;
   CGFloat dt = [[CCDirector sharedDirector] animationInterval]/(CGFloat)steps;

   for(int i=0; i<steps; i++){
       cpSpaceStep(space_, dt);
   }

   if (mySprite.getPhysicsBody->p.y > 500)
       [mySprite resetPosition];
}

now with Cocos2d v3 mySprite.physicsNode.position doesn't change through the time.

Any idea or link with some example?

Thanks.

1

1 Answers

0
votes

physicsNode.position doesn't change with time because it uses its parent sprite coordinate space, not the global coordinate space.

You can get the global position of any node, considering the Anchor Point, using this:

CGPoint worldPos = [node convertToWorldSpaceAR:CGPointZero];

After that you can easily convert it to any other node space if necessary (like your level, maybe) using:

CGPoint position = [_levelNode convertToNodeSpaceAR:worldPos];

But beware that you shouldn't hardcode the screen size on your code, as it varies for each device. You can use instead:

CGSize viewSize = [[CCDirector sharedDirector] viewSize];