Implementing Jumping in my 2d Side-scroller

Started by
5 comments, last by CannoN346 22 years, 2 months ago
Ok, im surrently working on a scroller. Im trying to implement jumping using the principles from the physics chapter from TOTWGPG. I can make the skelaton move up but i need to make it so that he can only jump while on the ground. Heres my code
  
// model gravity

skelaton.y+=skelaton.yv;
skelaton.yv+=gravity;

if ((keyboard_state[DIK_SPACE]) && (deb_key==0))
{
	skelaton.y-=skelaton.yv;
	deb_key=1;
}

if ((keyboard_state[DIK_SPACE]) && (deb_key==1))
skelaton.y+=skelaton.yv;
  
Using this he cant jump at all. if i add an "else" it can jump even while in the air. So, i added this code to the collision function...
  
// collision test

while(Collision_Test(skelaton.x,skelaton.y,skelaton.width,skelaton.height,
	  0,384,SCREEN_WIDTH*2,SCREEN_HEIGHT))
{
	  skelaton.y-=1;
          deb_key=0;
}
  
This did nothing. And lastly i tried this block of code and this didnt work.
  
if (skelaton.y <= 383)
deb_key=0;
else
if (skelaton.y > 383)
deb_key=1;
  
I had similar problems like this before and this technique worked. Now im stuck. Any suggestions??
Advertisement
You could set a flag that indicates the character is allowed to jump. Set it to false when they jump, and back to true when they land.

(P.S. Did you mean skeleton?)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
bool bJumping = false;if(jump_key){  if(!bJumping)  {    // jump routine  }} 


I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
I didn''t look at all your code but I would quickly suggest that since .yv is always decreasing while the person is jumping somewhere you must reset .yv to an initial start value before a jump. So you could just check if .yv == the inital start value then allow the jump. If not, then don''t.
Ok, thank you, i will try those methods. LoL, yes magmai, it should be skeleton.
The way I did my jump is here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=78723



EDIT:
oops. sorry. I didn't read the topic well (still sleepy from last night). anyways, what I just sent is how i implemented my jump not on landing. bad s_cloudx, bad.

Edited by - s_cloudx on February 17, 2002 6:39:24 PM
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
Did it! s_cloudx, i used a method similar to yours and i got my skeleton jumping. But after each jump, the jump would be shorter. It was the velocity that was causing this, because when the skeleton fell the velocity would be added to the gravity, so i simply reset the velocity everytime the skeleton hit the ground, as suggested by Brandisco. Anyways, thanks to everyone that replied.

This topic is closed to new replies.

Advertisement