Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

share|improve this answer

Define this:

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


m = Mouse.GetState();

        if (spY < surface)
            spY += gravity;
            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;



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


I just tried it, it works fine

share|improve this answer

Take a look at this tutorial: the site is also full of really useful tutorials related to XNA.

share|improve this answer
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 =]

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.