Cocos2d - 一个结束后更改动画结束后、动画、Cocos2d

由网友(单枪匹马.)分享简介:我有一个具有三个动画的 CCSprite:空闲、行走和攻击.我想根据精灵是否在移动(如果正在使用操纵杆)在空闲和行走之间切换.所有这一切都很好.对于攻击动画,我希望它运行一次,然后在完成时返回上一个动画(例如:空闲)我如何检测动画何时完成?I've got a CCSprite with three animatio...

我有一个具有三个动画的 CCSprite:空闲、行走和攻击.我想根据精灵是否在移动(如果正在使用操纵杆)在空闲和行走之间切换.所有这一切都很好.对于攻击动画,我希望它运行一次,然后在完成时返回上一个动画(例如:空闲)我如何检测动画何时完成?

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?

谢谢,

戴夫

推荐答案

好吧,这就是我所做的,它有效,虽然我不知道它是正确的还是最好的方法:1) 存储当前动画.2) 如果 currentAnimation 正在攻击,什么也不做.3)在动画之间切换时,如果新动画正在攻击,则在精灵上运行一个序列,第二个动作是对onDoneAttacking"的回调4)在回调中,改变当前动画

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.

这是它的样子:

 -(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";
}
阅读全文

相关推荐

最新文章