The Wizard - Part 2

Making a Sprite Jump

The Wizard - Part 2

Making a Sprite Jump

 

This is the second in a four part series to show you how to add some movement and action to your sprite. This first part was covered in "The Wizard - Part 1: Moving a Sprite on the Screen". Part two covers adding the ability to make your sprite Jump.

Starting the game project:

We are going to be building off everything that we did in the first part of this series  "The Wizard - Part 1: Moving a Sprite on the Screen". Instead of creating a new XNA Game project, you can just download the source for that project and begin working from that game project.

Enhancing the Wizard class:

All of our work to add the ability for our Wizard sprite to jump will be done by enhancing the Wizard class. Let's start off by adding a new state for the Wizard, we already had walking, now lets add a new state "Jumping"

Modify the State enum in the Wizard.cs class to look like the following.

        enum State
        {
            Walking,
            Jumping
        }
        State mCurrentState = State.Walking;

We're still starting out with the state being Walking, but Jumping is now an available State in code.

Next, we're going to add a new object called "mStartingPosition" to the Wizard class. This object will be used to track the position the Wizard sprite was in when it started it's jump.

Add the following object to the top of the Wizard.cs class

        Vector2 mStartingPosition = Vector2.Zero;

Now we need to modify our Update method to handle the situation of the Wizard sprite jumping. Modify the Update method in the Wizard.cs class to look like the following.

        public void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();
 
            UpdateMovement(aCurrentKeyboardState);
            UpdateJump(aCurrentKeyboardState);
 
            mPreviousKeyboardState = aCurrentKeyboardState;
 
            base.Update(theGameTime, mSpeed, mDirection);
        }

We added a new method call to "UpdateJump", but we don't have a method in our Wizard class called "UpdateJump", let's go ahead and add that now.

Add the following "UpdateJump" method to the Wizard.cs class.

        private void UpdateJump(KeyboardState aCurrentKeyboardState)
        {
            if (mCurrentState == State.Walking)
            {
                if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
                {
                    Jump();
                }
            }
 
            if (mCurrentState == State.Jumping)
            {
                if (mStartingPosition.Y - Position.Y > 150)
                {
                    mDirection.Y = MOVE_DOWN;
                }
 
                if (Position.Y > mStartingPosition.Y)
                {
                    Position.Y = mStartingPosition.Y;
                    mCurrentState = State.Walking;
                    mDirection = Vector2.Zero;
                }
            }
        }

Let's go over what this method is doing. First, it checks to see if the Wizard is currently walking, if it is, then it checks to see if the Spacebar has been pressed again (if it was held down from last time, then we don't want to jump). If the Spacebar was pressed (and previously released), then it calls the Jump method. So we only jump, if we were walking and the player has hit the Spacebar.

Next, we take care of the situation of when the Sprite is already Jumping (it's state will now be "Jumping"). The first check we do is to see if Y value of the position the Wizard was in when it started jumping, is greater than 150 (ooo, Magic number! We should have cleaned that up! Looks like a good exercise for you to improve this code! Always look for places like that in your code to improve!), then we change the Direction of the jump to start increasing along the Y axis. So basically what is happening is we are checking to see if the Wizard has reached the Height of his jump, if he has, then start making him head back down.

The second check we do is to see if the Wizard has moved down below the "ground" or starting position. If they have, then their current Y position will be greater than the position they were in when they started the Jump. When that happens, set their current Y position to the starting position of the jump (so they don't drop below the "ground" level), change the State to walking to indicate they have completed the Jump and zero out the Direction to indicate they're not moving in any direction for the Jump anymore.

The UpdateJump method called a "Jump" method when the player hit spacebar. So now we need to write the Jump method. Add the following "Jump" method to the Wizard.cs class.

        private void Jump()
        {
            if (mCurrentState != State.Jumping)
            {
                mCurrentState = State.Jumping;
                mStartingPosition = Position;
                mDirection.Y = MOVE_UP;
                mSpeed = new Vector2(WIZARD_SPEED, WIZARD_SPEED);
            }
        }

The Jump method first checks to make sure the Wizard currently isn't jumping. We don't want to "re-jump" them mid-jump. If they aren't currently Jumping, then change the state of the Sprite to Jumping, store the starting position, indicate that the sprite is going to be moving in the up direction and set the speed.

That's it! Go ahead and do a Build CropperCapture26324_thumb and move and jump your Wizard around the screen. You should be able to stand still and jump straight up and move to the left and right and hit the spacebar to jump to the left and to the right.

CropperCapture2703

 

Congratulations! You have successfully figured out how to make a sprite jump around the screen in your game. What modifications can you make? Can you increase how high the Wizard can jump? Can you change it so you have to hit the "J" key to Jump? Can you think of some other things you can change?

Play with it, experiment and ask questions. Most of all make sure you’re having fun!

Technorati Tags: