0

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!

Hyder
  • 1,163
  • 2
  • 13
  • 37
  • 3
    i guess your animation stops because of [plane stopAllActions]; do you intend to only stop the plane fly up action here? – Abhineet Prasad Sep 23 '14 at 18:01
  • @AbhineetPrasad You're right! Thanks! If I remove this `[plane stopAllActions];` line, the animation doesn't stop :) But my plane keeps on going down! How do I stop it from going down :( – Hyder Sep 23 '14 at 18:27
  • 1
    you can stop specific existing running actions by using plane stopActionByTag method. You will have to assign tags to the different actions and then stop them. Review your game logic and see which actions need to be stopped as per your requirement. At a cursory glance it seems that you should be able to use the physics engine to get the desired effect more easily. May be that is something you can explore as well. – Abhineet Prasad Sep 23 '14 at 20:40
  • @AbhineetPrasad Yes I did try stopping the specific actions by tag/name, but nothing helped. The plane still keeps going down. Can you elaborate a little what do you mean by "achieving the effect through physics engine", so that I know what to look up for. Thanks! – Hyder Sep 24 '14 at 04:04

0 Answers0