Jumping
By RUCCUS of RUCCUS.net
<EDIT> I've got an error to fix, the system works for one person fine</EDIT>
<EDIT 2> Ack, basically if you want the system to work for multiple objects take it out of the function and name each variable diferently, if that makes sense. I suck at using types so this might take a while to make it compatbile for more than one object.</EDIT 2>
When I was starting out I had troubles with creating some basic jumping code, so here it is. Gravity isn't supplied so you'll have to work that out for yourself, but this should be a good starter.
Just include this code before the main loop:
GLOBAL JUMP#
GLOBAL JUMPING#
GLOBAL STARTJUMP#
GLOBAL ENDJUMP#
And then add this code after the main loop:
FUNCTION JUMP(OBJECT#,SPEED#,HEIGHT#)
IF JUMP#=1 AND JUMPING#=0
JUMPING#=1
GROUND#=OBJECT POSITION Y(OBJECT#)
INC HEIGHT#,GROUND#
STARTJUMP#=1
JUMP#=0
ENDIF
IF STARTJUMP#=1
IF OBJECT POSITION Y(OBJECT#)<HEIGHT# THEN MOVE OBJECT UP OBJECT#,SPEED#
IF OBJECT POSITION Y(OBJECT#)>=HEIGHT# THEN STARTJUMP#=0:ENDJUMP#=1
ENDIF
IF ENDJUMP#=1
IF OBJECT POSITION Y(OBJECT#)>GROUND# THEN MOVE OBJECT DOWN OBJECT#,SPEED#
IF OBJECT POSITION Y(OBJECT#)<=GROUND# THEN ENDJUMP#=0:JUMPING#=0:JUMP#=0
ENDIF
ENDFUNCTION
Finally, call the JUMP function every loop. To activate the JUMP function, simply set JUMP# to 1. The function will automatically jump once and then make JUMP# equal 0 again.
SYNTAX:
JUMP(Object Number, Jump Speed, Jump Height)
Im working on adding in some basic physics so the user slows as they reach the height extent and then speed up again as they fall down. I should have it done soon, but no guarantees. Until then if you need physics im sure you can come up with a formula of your own
An example of using the function in the loop:
DO
IF SPACEKEY()=1 THEN JUMP#=1
JUMP(1,1,100)
SYNC
LOOP
Tips:
- Make sure you over ride the jump function when you land on something or the function will keep moving you down until you get to ground level.
How? Just make the program call the JUMP() function when the user isn't colliding with anything below them. And when the user does collide with something below them, reset the JUMP# variable to 0.
- The user only falls until they reach their ground level or original y height when they pressed the spacekey. So if the user is up high and jumps off of a ledge the user will fall only to their previous y, and will then float in the air.
How can I fix this? With the implementation of some basic gravity the system will keep moving the user down. I'd suggest moving the user down at all times as long as they are
1. Not standing on anything
2. Not jumping
But thats just a suggestion since gravity functions differ a lot from program to program.

Current Projects: SHADE - Game Maker | Mecho - TGC Puzzle Entry | Halo Physics Engine | COLD - Polygonal Collision Detection