Cocos2d - 一个结束后改变动画 [英] Cocos2d - Changing animations after one is over
问题描述
我有一个CCSprite有三个动画:空闲,步行和攻击。我想在空闲和步行之间切换,取决于子画面是否移动(如果操纵杆正在使用)。所有这一切都很好。
对于攻击动画,我想让它运行一次,然后在完成时返回到上一个动画(例如:idle)如何检测动画是否完成?
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?
感谢,
Dave
推荐答案
,所以这里是我做了,它的工作,虽然我没有线索,如果它的权利或最好的方式:
1)存储当前动画。
2)如果currentAnimation正在攻击,什么都不做。
3)当在动画之间切换时,如果新动画正在攻击,在sprite上运行一个序列,第二个动作是回调一个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屋!