Cocos2d - Changing animations after one is over

二次信任 提交于 2019-12-13 16:11:37

问题


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


回答1:


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";
}


来源:https://stackoverflow.com/questions/3273147/cocos2d-changing-animations-after-one-is-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!