AS3:仅按一键

时间:2013-05-31 17:58:32

标签: actionscript-3 keypress

我正在尝试创建一个平台游戏,我有跑步,跳跃和双跳工作。现在我的问题是我希望玩家按下跳跃按钮和英雄只跳一次而不是连续跳跃。如果我按住向上按钮它将继续跳跃,我不想要,我只是希望它执行跳跃一次,即使按键仍然按下。我想要双跳一样,我怎么能这样做。

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

    public class Player extends MovieClip
    {
        //Player run speed setting
        var RunSpeed:Number = 8;
        //Player key presses
        var RightKeyPress:Boolean = false;
        var LeftKeyPress:Boolean = false;
        var UpKeyPress:Boolean = false;
        //Jump variables
        var Gravity:Number = 1.5;
        var JumpPower:Number = 0;
        var CanJump:Boolean = false;
        var DoubleJump:Boolean = false;

        public function Player()
        {
            // constructor code
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
            addEventListener(Event.ENTER_FRAME,Update);
            stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);
        }

        function KeyPressed(event:KeyboardEvent)
        {
            //When Key is Down
            if (event.keyCode == 39)
            {
                RightKeyPress = true;
            }

            if (event.keyCode == 37)
            {
                LeftKeyPress = true;
            }

            if (event.keyCode == 38)
            {
                UpKeyPress = true

            }
        }

        function Update(event:Event)
        {
            //Adding gravity to the game world
            JumpPower +=  Gravity;
            //if player is more than 300 on the y-axis
            if (this.y > 300)
            {
                //Player stays on the ground and can jump
                JumpPower = 0;
                CanJump = true;
                DoubleJump = false;
            }

            //If player is on floor and right key is pressed then run right
            if ((RightKeyPress && CanJump))
            {
                x +=  RunSpeed;
                gotoAndStop('Run');
                scaleX = 1;
            }
            else if ((LeftKeyPress && CanJump))
            {
                //Otherwise if on floor and left key is pressed run left
                x -=  RunSpeed;
                gotoAndStop('Run');
                scaleX = -1;
            }
            else if ((UpKeyPress && CanJump))
            {
                //Otherwise if on floor and up key is pressed then jump
                JumpPower = -15;
                CanJump = false;
                gotoAndStop('Jump');
                DoubleJump = true;
            }

            //If on floor and right and up key are pressed then jump
            if ((RightKeyPress && UpKeyPress && CanJump))
            {
                JumpPower = -15;
                CanJump = false;
                gotoAndStop('Jump');
                DoubleJump = true;
            }

            //If on floor and left and up key are pressed then jump
            if ((LeftKeyPress && UpKeyPress && CanJump))
            {
                JumpPower = -15;
                CanJump = false;
                gotoAndStop('Jump');
                DoubleJump = true;
            }

            //If jumped and right key is pressed then move right
            if ((RightKeyPress && CanJump == false))
            {
                x +=  RunSpeed;
                scaleX = 1;
            }
            else if ((LeftKeyPress && CanJump == false))
            {
                //Otherwise if jumped and left key is pressed then move left
                x -=  RunSpeed;
                scaleX = -1;
            }

            //If in air and able to doublejump and pressed up key, then double jump
            if (UpKeyPress && DoubleJump && JumpPower > -2)
            {
                JumpPower = -13;
                DoubleJump = false;
                gotoAndStop('DoubleJump');
            }

            //If on floor and no key is presses stay idle
            if ((!RightKeyPress && !LeftKeyPress && CanJump))
            {
                gotoAndStop('Idle');
            }

            this.y +=  JumpPower;
        }

        function KeyReleased(event:KeyboardEvent)
        {
            if (event.keyCode == 39)
            {
                event.keyCode = 0;
                RightKeyPress = false;
            }

            if (event.keyCode == 37)
            {
                event.keyCode = 0;
                LeftKeyPress = false;
            }

            if (event.keyCode == 38)
            {
                event.keyCode = 0;
                UpKeyPress = false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我怀疑发生的事情是,只要你的玩家的y值满足条件if (this.y > 300),你就可以让他再次跳跃,所以只要按下按键他就会跳跃,因为{{1}都是真的。

我的猜测是,做以下事情可能会让你更接近你的答案......

在您的更新功能中:

UpKeyPress && CanJump

然后在你的KeyPressed函数中:

function Update(event:Event)
{
    //Adding gravity to the game world
    JumpPower +=  Gravity;
    //if player is more than 300 on the y-axis
    if (this.y > 300)
    {
        //Player stays on the ground and can jump
        JumpPower = 0;
        // Do not allow another jump until the UpKey is pressed
        //CanJump = true;
        //DoubleJump = false;
    }
    ...

我也是第二个shaunhusain建议设置断点并熟悉调试代码。