简体   繁体   中英

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.

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

Try following:

if(Input.GetKeyDown(KeyCode.W))
{
    // your code here
}

It will make it so that every time the player presses W , they will only go until they can press W again. You can always set Rate of Jump and make it so that they can only jump once every so and so seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM