我有一个带有三个动画的CCSprite:空闲,行走和攻击.我想在空闲和步行之间切换,具体取决于精灵是否在移动(如果操纵杆正在使用).所有这一切都很棒.对于攻击动画,我希望它运行一次,然后在完成后返回上一个动画(例如:空闲)如何检测动画何时完成?
谢谢,
戴夫
好吧,所以这就是我所做的,并且它有效,虽然我不知道它是正确还是最好的方式:1)存储当前动画.2)如果currentAnimation正在攻击,则什么也不做.3)当动画之间切换时,如果新动画正在攻击,则在精灵上运行一个序列,第二个动作是回调到回调中的"onDoneAttacking"4),改变当前动画
但这并不是很顺利,它不允许快速攻击.
这是它的样子:
-(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";
}
Run Code Online (Sandbox Code Playgroud)