在as3 flash中攻击动画问题

时间:2016-05-16 00:31:35

标签: actionscript-3 flash animation

  

我的问题是,当我按下空格键时,它会播放攻击动画,但是一旦我发布它就会停止动画过程。我想要它只需按下并释放按钮,然后它就会自动完成动画。

function keyPressed(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = true;  
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = true;
          }
        if(e.keyCode == Keyboard.UP && onGround)
          {
             jumped = true;
          }
        if(e.keyCode == Keyboard.DOWN && onGround)
          {
             crouchMode = true; 
          }
        if(e.keyCode == Keyboard.SPACE && onGround)
          {
              attackMode = true;

          }
    }
    function keyReleased(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.RIGHT)
          {
             rDown = false; 
          }
        if(e.keyCode == Keyboard.LEFT)
          {
             lDown = false;  
          }
        if(e.keyCode == Keyboard.UP)
          {
             jumped = true;  
          }
        if(e.keyCode == Keyboard.DOWN)
          {
             crouchMode = false; 
          }
        if(e.keyCode == Keyboard.SPACE)
          {
             attackMode = false; 
          }
    }
    function gameLoop(e:Event):void
    {   
        playerStart();

              }  
        }
    }
    function playerStart():void
    {
        warMage.y += grav;//Apply gravity to player

        collisionStageCheck();

        if(crouchMode)
        {
            warMage.gotoAndStop("CrouchWarmage");
            rDown = false;
            lDown = false;
        }
        if(attackMode)
        {

            if(warMage.scaleX == 1)//If we face right
            {
              warMage.x += 5;//Lunge right
            }
            if(warMage.scaleX == -1)//If we face left
            {
              warMage.x -= 5;//Lunge left   
            }
            warMage.gotoAndPlay("AttackWarmage");
        }
        if(rDown)//If we move right
        {
           warMage.x += 12;
           warMage.gotoAndStop("RunWarmage");
           warMage.scaleX = 1;
        }
        if(lDown)//If we move left
        {
           warMage.x -= 12;
           warMage.gotoAndStop("RunWarmage");
           warMage.scaleX = -1;
        }
        if(jumped)//If we jumped
        {
           warMage.y -= 30;
           warMage.gotoAndStop("JumpWarmage");
        }
        if(!rDown && !lDown && !jumped && !crouchMode && !attackMode)//If we are in neither states
        {
            warMage.gotoAndStop("idleWarmage");
        }
    }

1 个答案:

答案 0 :(得分:0)

攻击动画未完成,因为attackMode在空格键false功能上设置为keyReleased。不要这样做。相反,在动画的最后一帧上使attackMode为false。在嵌套在if (attackMode) {逻辑块中的Main类中执行此操作:

if (heroMage.currentFrame == "LastFrame") 
{
    attackMode = false;
}

显然,您可以命名攻击动画的最后一帧" LastFrame"。

我还发现了一种更优雅的方法here,使用eventDispatcher无需检查当前帧,但在影片剪辑时间轴内添加代码会增加一些麻烦(在我看来) 。