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