Question

I use the following to to interchange only the position of 2 sprite.

CCSprite *sprite1 = (CCSprite*)[self getChildByTag:tagOfFirstSprite]; 
    CCSprite *sprite2 = (CCSprite*)[self getChildByTag:tagOfSecondSprite]; 

    CGPoint SpritePosition1 = [sprite1 position];  
    CGPoint SpritePosition2 = [sprite2 position];  



    [sprite1 runAction:[CCMoveTo actionWithDuration:1.0 position:ccp(SpritePosition2.x, SpritePosition2.y)]];
    [sprite2 runAction:[CCMoveTo actionWithDuration:1.0 position:ccp(SpritePosition1.x, SpritePosition1.y)]];

These are box2d body.But it doesn't work.......any idea??

Was it helpful?

Solution

I assume that you are using Box2D sample template which implements the "tick()" function that is continuously called by the scheduler. This function calls the world->step() function and the updates the position of the box2D bodies and then update the position of your sprites according to the new position of box2D bodies. When you just call the runAction, it doesn't effect the position of your Box2D bodies, so each time tick() function will be called, the position of your sprites will be set according to the position of box2D bodies. Now even if your runAction is moving your sprites, it gets reset after couple of times every second when tick() function is called. I hope you understand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top