0

我正在尝试创建一个节拍游戏,现在我的角色正在攻击。到目前为止,我有 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;
4

1 回答 1

0

首先,您应该删除添加和删除的事件侦听器KeyPressedKeyUp方法(它们不是必需的,它们只会引起问题),并像其他两个一样在构造函数中注册按键事件。

其次,要做到这一点,您需要检查按键是否按下而不是听按键。为此,您将需要一个名为的新字段,例如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++;
        }
        //....
于 2013-08-04T00:29:54.097 回答