1

我试图创建一个平台游戏,但我在游戏的跳跃部分苦苦挣扎。我已经完成了跳跃,但是当我尝试添加动画时,一切都出错了。当我按下跳跃按钮时,它会跳跃,但当我着陆时,它会卡在跳跃动画的末尾,直到我移动。其次,当我跳跃并按下右或左按钮时,它会跳跃,但会在我尝试在半空中移动时播放运行动画。我该如何解决这些问题

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 Yvelocity:Number = 0;
        var CanJump: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
            Yvelocity +=  Gravity;
            //if player is more than 300 on the y-axis
            if (this.y > 300)
            {
                //Player stays on the ground and can jump
                Yvelocity = 0;
                CanJump = true;
            }

            if ((RightKeyPress == true))
            {
                x +=  RunSpeed;
                gotoAndStop('Run');
                scaleX = 1;
            }
            else if ((LeftKeyPress == true))
            {
                x -=  RunSpeed;
                gotoAndStop('Run');
                scaleX = -1;
            }

            if ((UpKeyPress == true && CanJump))
            {
                Yvelocity = -15;
                CanJump = false;
                gotoAndStop('Jump');
            }
            this.y +=  Yvelocity;
        }

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

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

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

2 回答 2

1

在执行这些块之前,只需检查左右按键上的 canJump 是否为真。如果 canJump 为真,则允许运行的东西

于 2013-05-25T15:58:24.693 回答
1

this.y > 300,这是你的跳转结束逻辑。添加 gotoAndStop('Idle'); 在那个if的块。

于 2013-05-24T23:35:20.140 回答