0

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

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;
            }
        }
    }
}
4

1 回答 1

2

我怀疑正在发生的事情是,一旦您的玩家的 y 值满足if (this.y > 300)您使他再次跳跃的条件,只要按住键,他就会跳跃,因为UpKeyPress && CanJump两者都是真实的。

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

在您的更新功能中:

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;
    }
    ...

然后在您的 KeyPressed 函数中:

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
        // Do not allow another jump until the UpKey is pressed
        if (this.y > 300)
        {
            CanJump = true;
            DoubleJump = false;
        }
    }
}

我还赞同 shaunhusain 的建议,即设置断点并熟悉调试代码。

于 2013-06-01T04:30:46.090 回答