Search code examples
c#xnasprite

How to make my sprite Jump? (XNA, C#)


I've been working on a simple platformer game using XNA. I have been having some trouble working out a good jumping algorithm. After getting rid of the messy half done versions of the jumping algorithm all I have left is.

if (keystate.IsKeyDown(Keys.W))
{
    spritePosition.Y = spritePosition.Y - 10;
}

I realise this is a very unsophisticated start but any tips would be great. I'm a self taught programmer so some terminology might be lost on me, I tend to find looking at actual code the best way to learn. Thanks.


Solution

  • When you jump (physically) you are adding an amount to your Y velocity vector, not to your position vector. Moving your position by 10 means you're just teleporting upward 10 ticks.

    Try adjusting velocity, then letting your main loop change the position. And remember acceleration downward from gravity!

    Edit: Added link from comment below to physics tutorial. rodedev.com/tutorials/gamephysics