1
votes

Im trying to create a platform game but im struggling with the jump part of the game. I have got the jump working but when I try to add the animation it goes all wrong. When I hit the jump button it jumps but when i lands it gets stuck on the end of the jump animation until i move. Secondly when i jump and press the right or left button it jumps but plays the run animation while i try to move in mid air. How can i resolve these problems

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

2 Answers

1
votes

this.y > 300, that is your jump end logic. Add a gotoAndStop('Idle'); in that if's block.

1
votes

Just Check if canJump is true on your left and right keypresses before executing those blocks. If canJump is true allow the running stuff