AS3攻击动画问题

时间:2013-08-03 23:34:59

标签: actionscript-3

我正在尝试创建一个节拍游戏,现在我的角色攻击了。到目前为止我有3个攻击动画,并且工作正常。如果你继续捣碎攻击按钮它会攻击,但问题是它会在攻击按钮关闭后立即进入下一个攻击动画,我不想这样做。一旦当前的攻击动画完成而不是跳到当前动画中间的下一帧,我怎样才能进入下一个攻击动画。所以我希望角色完成攻击,如果玩家仍然按下攻击键,它将进入下一个攻击框架。

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class Player extends MovieClip
    {
        //Attack variables
        var Attacking:Boolean = false;
        var Punches:int = 0;
        var Punching:Boolean = false;
        var Kicks:int = 0;
        var Kicking:Boolean = false;

        public function Player()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
            addEventListener(Event.ENTER_FRAME,Update);
        }

        function KeyPressed(event:KeyboardEvent):void
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);

                //If A key is down
                if (event.keyCode == 65)
                {
                    Attacking = true;
                    Punching = true;
                    Punches++;
                }
        }

        function Update(event:Event)
        {
            //If player is not attacking
            if (Attacking == false)
            {
                Punching = false;
                Punches = 0;
                Kicking = false;
                Kicks = 0;
            }   
            else if (Attacking == true)
            {

                if (Punching == true)
                {
                    gotoAndStop('Jab' + Punches);
                }
            }

        }


        function KeyUp(event:KeyboardEvent)
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

        }
    }
}

同样在我放下的每个攻击动画的最后一帧内

import flash.display.MovieClip;
import flash.events.Event;

stop();
MovieClip(parent).Attacking = false;
MovieClip(parent).Punches = 0;

1 个答案:

答案 0 :(得分:0)

首先,你应该删除事件监听器添加和删除KeyPressedKeyUp方法(它们不是必需的,它们只会导致问题)并在costructor中注册key up事件就像其他两个一样。

其次,要完成此操作,您需要检查按键是否关闭而不是按下按键。为此,您需要一个名为holdingAttackKey的新字段。

        var holdingAttackKey:Boolean = false;  



        function KeyPressed(event:KeyboardEvent):void {
            if (event.keyCode == 65)
            {
                holdingAttackKey = true;

                Attacking = true;
                Punching = true;
                //...



    function KeyUp(event:KeyboardEvent)
    {
        holdingAttackKey = false;
    }



    function Update(event:Event)
    {
        if(holdingAttackKey && Attacking==false) {
                Attacking = true;
                Punching = true;
                Punches++;
        }
        //....