1

I want to be able to stop all actions when my animation sequence is done running, how would I do that? Right now i have:

CCAnimation *spinAnim = [CCAnimation
                                 animationWithSpriteFrames:spinAnimFrames delay:0.1125f];
        self.spinAction = [CCAnimate actionWithAnimation:spinAnim];

and then later:

[self.character runAction:self.spinAction];

So how would I be able to tell that the spin action is finished?

1
  • In cocos2d-iphone, You can use either CCCallFunc of CCCallBlock to get notify that CCAction is over. Sep 18, 2013 at 5:10

3 Answers 3

3

You could use a sequence

CCCallFunc *callMe = [CCCallFunc actionWithTarget:self selector:@selector(doneSpin)];
CCSequence *seq    = [CCSequence actions:spinAnim,callMe,nil];
self.spinAction    = seq;

[self.character runaction:self.spinAction];

and elsewhere in your code :

-(void) doneSpin {
    // spin action done, do whatever here !

}
4
  • For some reason this method was giving me an error saying spinAnim was an undeclared identifier. Sep 21, 2013 at 17:38
  • Ohhhhhhh ok all I would need to do is make it global and that should work, I'll try that too Sep 21, 2013 at 18:27
  • or put my lines to replace your definition for spinAction. A good way to handle these things is also to use blocks, i would encourage you to read-up on them.
    – YvesLeBorg
    Sep 21, 2013 at 18:30
  • Thanks! I'll do my research I appreciate everything! Sep 21, 2013 at 18:34
0

You can arrange a callback using CCCallFunc by adding it as the last action in a sequence of actions as follows :

[self.character runAction:[CCSequence actions:self.spinAction,[CCCallFunc actionWithTarget:self selector:@selector(stopAllActions)],nil]];

Hope it helps!

1
  • Unfortunately I don't have enough rep to upvote :( I could only choose 1 answer and his was a bit more in depth. Sep 21, 2013 at 20:25
0

I just realized it with blocks like this:

(on .h)

typedef void(^actionTerminated)(void);

(on .m)

-(void) doJump:(float)delay
         times:(int)times
   actionBlock:(actionTerminated)actionBlock {
   // here there is your code, physicsBody adjusts..

   id block = [CCActionCallBlock actionWithBlock:^
              { 
                // stop sound effect if there was
                actionBlock(); 
   }];
   actionSequence = [CCActionSequence actionWithArray:@[self.action,block]];
   [self.sprite runAction:actionSequence];
}

// when you call do jump :
[self doJump:0.45 times:1 actionBlock^{
    // here jump action is terminated,do whatever you want..
}];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.