views:

185

answers:

1

Hey,

I've got a CCSprite with three animations: idle, walk and attack. I want to switch between idle and walk depending on whether or not the sprite is moving (if the joystick is beeing used). All of this works great. For the attacking animation, I want it to run once, and then return to the previous animation when done (ex.: idle) how do I detect when the animation is done?

thanks,

Dave

A: 

Alright, so here's what i've done, and it works, although I have no clue if its the right or the best way: 1) store the current animation. 2) If the currentAnimation is attacking, do nothing. 3) when switching between animations, if the new animation is attacking, run a sequence on the sprite, the second action being a callback to a "onDoneAttacking" 4) in the callback, change the current animation

But this isn't veery smooth and it doesn't allow to attack very fast.

Here's what it looks like:

 -(void) changeAnimation:(NSString*)name forTime:(int) times {

    if(currentAnimation != @"attack" )
    {
        CCFiniteTimeAction *action = [CCAnimate actionWithAnimation:[self animationByName:name]];
        CCRepeat *repeatAction = [CCRepeat actionWithAction:action times:1];
        if(name == @"attack") {
            id doneAttacking  = [CCCallFunc actionWithTarget:self selector:@selector(onDoneAttacking)];
            [self runAction:[CCSequence actionOne:repeatAction two:doneAttacking]];
        }
        else {
            [self runAction:repeatAction];
        }
        currentAnimation = name;
    }
}
-(void) onDoneAttacking {
    currentAnimation = @"idle";
}
David Menard