Cocos2d - 一个结束后更改动画 [英] Cocos2d - Changing animations after one is over

查看:14
本文介绍了Cocos2d - 一个结束后更改动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有三个动画的 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";
}

这篇关于Cocos2d - 一个结束后更改动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆