Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How to jump in 2D

Author
Message
Gaelbin
15
Years of Service
User Offline
Joined: 16th Feb 2009
Location:
Posted: 17th Feb 2009 05:31
I was wondering about this problem. I have been programming on and off for the past year and I am wondering how to get a character to jump, that is, a key is pressed and the character goes up quickly, stops at some point and comes back down over a period of time. If there was anyone with any code snippet ideas, I would be greatly appreciative.

Thanks!
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 17th Feb 2009 07:38 Edited at: 17th Feb 2009 07:41
I don't have a code snippet for you unfortunately, but you could always try having a vertical velocity variable. It would possibly be a negative value equal to the gravity you want to use. Then when the jump key gets pressed you negate it (which would make it positive). Each frame you move the character up by the velocity and decrease the velocity by a little bit. Just keep doing this until your velocity equals your gravity again stopping downward movement when you hit the ground.

Maybe something like this:



Then in the game loop you'd always be applying the player's velocity to their vertical position. Also, you'd be lowering the velocity by 1 (or something like that) each cycle as long as it was above your gravity value. Obviously barriers such as floors and ceilings would have to be tested or you'd travel indefinitely.

Anyway, hope this helps a bit. Maybe somebody has some actual code for you.

-Frank
Comnik
15
Years of Service
User Offline
Joined: 16th Feb 2009
Location:
Posted: 17th Feb 2009 09:17
I think I can Help you.
First you need a CharSet with your Sprite.

Next you have to Create a Animation of the Sprite jumping.
Then something like this:

If Right Key is pressed: Increase the Sprites X Coordinate and play The Walking Animation, to make it Walk right.

If Left Key Pressed: Decrease Sprite X Coordinate and play the Walking Animation to make it walk left.

Now Jumping:
If UP Key is pressed: Decrease Sprites Y Coordinate and Play The Jump Animation, to Make it Jump.
________________________________________

So first you can try everything without Animations,
but It looks better
I will try to make an Example Code with Animations and other Stuff and post it here. But I'm not Sure...
Gatorules
15
Years of Service
User Offline
Joined: 17th Dec 2008
Location:
Posted: 17th Feb 2009 18:09 Edited at: 17th Feb 2009 18:10


adding in animations should be selfexplanatory
Gaelbin
15
Years of Service
User Offline
Joined: 16th Feb 2009
Location:
Posted: 17th Feb 2009 20:59 Edited at: 18th Feb 2009 16:13
Deleted post. Am currently working with the code

Thank you so much! (Still having a few problems making my guy come down though).
Gaelbin
15
Years of Service
User Offline
Joined: 16th Feb 2009
Location:
Posted: 18th Feb 2009 16:44
done and done. thank you so much. I am going to have some problems in the future in the way that I set things up, but I am pretty sure i can fix them when the time comes. You guys have been great. Thank you.

Login to post a reply

Server time is: 2024-11-25 13:36:01
Your offset time is: 2024-11-25 13:36:01