2D Platform Game Jumping

I'm currently writing a game in XNA for fun which uses C#. I have got my sprites loaded and when the character moves right he looks like he is running right and when he moves left he looks like he is running left.

I been looking everywhere for a good coding example for how to create a jumping ability. I have read all the physics stuff that I can stand and it doesn't help when I can't figure out how to use say space bar to jump yet can't keep them from using space just jump again until they land.

Create a variable, canJump and set it to true whenever your sprite is on a jumpable surface. If you are using a negative y velocity every frame then letting collision detection prevent you from falling through the ground then you can set canJump whenever the players sprite collides with terrain. Whenever the player jumps just set canJump to false and it will remain that way until collision detection changes it back

Define this:

int gravity = 2, jump=50, spX=20,spY=20, surface=200;
bool click = false;
bool canJump = true;
Texture2D sprite;
MouseState m;


---Update---

m = Mouse.GetState();

if (spY < surface)
{
spY += gravity;
}
else
{
if (spY == surface)
{
canJump = true ;
}
}
if (m.LeftButton == ButtonState.Pressed)
{
click = true;
}
if ((click) && (m.LeftButton == ButtonState.Released))
{
if (canJump)
{
click = false;
spY -= jump;
canJump = false;
}
}


---EndUpdate---

---Draw---

spriteBatch.Draw(sprite,new Vector2(spX,spY),Color.White);


---EndDraw---

I just tried it, it works fine

Take a look at this tutorial: http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml the site is also full of really useful tutorials related to XNA.

• XNADevelopment is a great site. – The Communist Duck Feb 13 '11 at 10:16
• Thank you that site actualy was what I was looking for. Now that I got an understanding and it works the way I want it too I can deal with gravity and what not. – Bradley Kreuger Feb 13 '11 at 18:42

What I've done int the past, is have your character hold a velocity in each direction. When I want a jump, I simply say vel_y -= (jump velocity here). And of course I have a constant gravity.

One other really nice feature you can should look into implementing is the whole concept of "the longer you hold the button the higher you go". I Hate it when games only jump one single constant variable, and don't allow me to have some feeling of control.

Oh, and wall jumps are cool =]