Josh-CO Dev

Solving the worlds problems one line of code at a time.

XNA – Adding a Jumping Sprite

Leave a comment


The next logical step in my project was to figure out how to make a sprite jump. I followed George Clingerman’s tutorial HERE and modified it a bit. I didn’t like that it kept making you jump along the path that you started, it just felt a bit fake, so I removed the jumping state and modified the code a bit. See the code below for the bulk of the logic.

private void updateJump(KeyboardState keyState)
{
if (keyState.IsKeyDown(Keys.Space) && !mPreviousKeyboardState.IsKeyDown(Keys.Space) && !jumping)
{
jump(); //start the jump
}

//if the character has reached 100 pixels higher, move them down
if (jumping)
{
if (base.position.Y startingPos.Y)
{
position.Y = startingPos.Y;
mDirection.Y = 0;
jumping = false;
}
}
}

private void jump()
{
jumping = true; //flag the jumping bool
startingPos = base.position; //set the origin postion to the current position

mDirection.Y = MOVE_UP;
mSpeed.Y = WIZARD_SPEED;
}

The next steps are to add some logic to make the jump a little more natural, such as adding an arc, and to add some gravity that will pull the character under the lower boundaries if there is not an object there. Hopefully tackle that this weekend.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s