I'm animating a sprite in my game (via sprite sheets etc.). It's a simple animation, which I want to happen throughout the life of game. The sprite is also a physics body. And when the screen is touched, I want sprite to perform some actions.
I've added the animation and the actions and everything is okay. But the problem is, the animation stops when the sprite performs other actions (when touched).
Here is my sprite code in the init
method:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"plane1.plist"];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i)
{
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"planee%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.1]; //Speed in which the frames will go at
//Adding png to sprite
plane = [CCSprite spriteWithImageNamed:@"plane1.png"];
//Positioning the sprite
plane.position = ccp(self.contentSize.width/2,self.contentSize.height/2);
//Repeating the sprite animation
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
//Animation continuously repeating
[plane runAction:repeatingAnimation];
//Adding the Sprite to the Scene
CGRect aRectangle = (CGRect){ccp(0,-10), plane.contentSize};
CGRect smallerRectangle = CGRectInset(aRectangle, 30, 20);
plane.physicsBody = [CCPhysicsBody bodyWithRect:smallerRectangle cornerRadius:0];
plane.physicsBody.collisionGroup = @"playerGroup";
plane.physicsBody.collisionType = @"planeCollision";
[_physicsWorld addChild:plane];
Here is my touchBegan
method (where other actions are performed when the screen is touched:
/*The following code makes the Plane go up when touched anywhere on the screen and then starts downward movement*/
[plane stopAllActions];
float currentRotation = plane.rotation;
if (currentRotation==10) {
[plane runAction:[CCActionSequence actions:
[CCActionRotateBy actionWithDuration:.1 angle:-30],
[CCActionMoveBy actionWithDuration:.1 position:ccp(0,30)],
[CCActionRotateBy actionWithDuration:.2 angle:30],
[CCActionCallBlock actionWithBlock:
^{
[self startDownMovement];
}],nil]
];
}
else {
[plane runAction:[CCActionSequence actions:
[CCActionMoveBy actionWithDuration:.1 position:ccp(0,30)],
[CCActionRotateTo actionWithDuration:.1 angle:10],
[CCActionCallBlock actionWithBlock:
^{
[self startDownMovement];
}],nil]
];
}
And here is the startDownMovement
code:
float currentRotation = plane.rotation;
if (currentRotation!=10) {
[plane runAction:[CCActionSequence actions:[CCActionRotateBy actionWithDuration:0.1 angle:30],
[CCActionMoveBy actionWithDuration:1 position:ccp(0,-plane.position.y)],
nil]];
}
else {
plane.rotation = 10;
[plane runAction:[CCActionMoveBy actionWithDuration:1 position:ccp(0,-plane.position.y)]];
}
Please help me. I have no idea why my animation stops when I tap the screen to fly the plane up.
Thanking in anticipation!