ActionScript3.0按下按钮后停止角色动画

时间:2017-04-19 18:57:22

标签: actionscript-3 flash actionscript character

我有一个角色(myHero),里面有几个动画,闲着,走路,转身,跳跃,攻击和跳舞。 使用我现在正在使用的代码,我需要按住按钮才能使整个动画工作(当你走路时这很好但是当我想要攻击时,我希望按钮按下一次,整个动画也会播放ONCE) 截至目前,需要持续按下按钮才能播放动画。 这是我的代码:

var keyRight:Boolean=false;
var keyLeft:Boolean=false;
var keyUp:Boolean=false;
var keyAttack:Boolean=false;
var keyDance:Boolean=false;


myHero.gotoAndStop(1);//the idle animation

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_Handler)
function keyDown_Handler(event:KeyboardEvent):void
{

    if(event.keyCode == Keyboard.X)
    {
        keyAttack=true;
    }
//theres more code for the other keys all similar to this

}

stage.addEventListener(KeyboardEvent.KEY_UP, keyUp_Handler)
function keyUp_Handler(event:KeyboardEvent):void
{

    if(event.keyCode == Keyboard.X)
    {
        myHero.gotoAndStop(1);//the idle animation
        keyAttack=false;
    }
//theres more code for the other keys all similar to this

}

有了这个,我必须继续按下键。所以我试着把它添加到keyDown_Hander中:

if(event.keyCode == Keyboard.X)
{
    if(myHero.Attack.currentFrame==8)//the animation is 8 frames long
    {
        myHero.gotoAndStop(1);//the idle animation
        keyAttack=false;
    }   
}

但是现在当我按下键时动画会永远循环,除非我用另一把键取消它。我也试过添加stop();到动画的最后一帧,但它只是冻结在最后一帧,并没有像我想要的那样返回到空闲状态。

任何帮助?

1 个答案:

答案 0 :(得分:0)

您可以为要按下和释放的任何键盘键执行一个选项并完成动画,只需设置布尔值,而不是告诉动画转到空闲帧。因此,对于攻击密钥,只将keyAttack boolean设置为false。

stage.addEventListener(KeyboardEvent.KEY_UP, keyUp_Handler)
function keyUp_Handler(event:KeyboardEvent):void
{

    if(event.keyCode == Keyboard.X)
    {
        keyAttack=false;
    }
//theres more code for the other keys all similar to this

}

然后在动画(MovieClip)中,您可以在8帧结尾处获得一段代码,用于检查布尔值是否为false。我假设攻击动画位于上面示例文本中名为myHero的MovieClip中。

// At the last frame of the attack animation inside myHero.Attack
if (MovieClip(this.parent.parent).keyAttack == false){
   // Assumming that this.parent = myHero
   // and this.parent.parent = the main stage where keyAttack varable lives
    MovieClip(this.parent).gotoAndStop(1); // though I would use a frame label called ("idle")
} else {
    this.gotoAndPlay(1); // replay attack animation assuming that the X key is still pressed down
}