Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

4 Answers 4

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;

---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

share|improve this answer

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.

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

 
discard

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.