You need to set some flags that mean that the player has to release the jump key in order to initiate another jump. However, the exact nature of how you do this depends on the game play that you want. SO for instance, do you want:
1) Press and hold jump key
2) Player jumps up then falls
3) Upon landing player jumps up again as they have the jump key pressed
or
1) Press and hold jump key
2) Player jumps up then falls
3) Player lands and then play has to release then press the jump key again in order to jump up
There isn't a right answer, it's a matter of how you want the game to play. I've modified Kezzla's code to do the latter.
//setup 3d
sync on
sync rate 60
autocam off
backdrop on
color backdrop 0
position camera 0,72,-200
gravity# = 0.1
//make actor
make object cube 1,10
//make and color ground plane
make object plane 2,500,500,1
rotate object 2,90,0,0
position object 2,0,-5,0
color object 2,rgb(0,55,0)
do
center text screen width()/2,20,"up arrow to jump, left and right arrows to walk"
//control accelleration of gravity.
if force# > -3 and force# < 3 then force# = force# - gravity#
//if upkey = 1 then reverse the direction of the force
//player must release the upkey in order to initiate another jump
//play must land before initiating another jump
if jumping = 0
if upkey_pressed = 0
if upkey() = 1 and force#<3
force# = 2
upkey_pressed = 1
jumping = 1
endif
endif
if upkey() = 0 then upkey_pressed = 0
endif
//position object and add the force value to the objects Y position.(usually negative)
position object 1,object position x(1),object position y(1)+force#,object position z(1)
//if object hits ground stay there - better handled with a collision system, just here for simple example
if object position y(1)<0
position object 1,object position x(1),0,object position z(1)
`jumping = 0
endif
//walk left and right
if rightkey() = 1 then move object right 1,1
if leftkey() = 1 then move object left 1,1
SYNC
loop
If you take out all the stuff to do with the variable "jumping" then you can chain together infinite jumps by constantly pressing-releasing-pressing the jump key.
Below is the method I use for jumping, this version happens to include double jumping (I can strip that out if you want).
sync rate 60
sync on
hide mouse
`some constants
rad# = 50 :`radius of sphere
jspd# = 20 :`initial speed of jumping when the spacebar is press
fall_speed_jump_limit# = -10.0 :`varying this number will essentially change the "window of oportunity" the player has in order to initiate another jump
max_number_of_jumps = 2 :`this is the maximum number of jumps the player can chain together
grav# = 0.5 :`gravity
`make a ground for reference
make matrix 1, 2000,2000, 10,10
`make a jumping object
make object sphere 1, rad#*2
color object 1, rgb(255,0,0)
position object 1, 1000, rad#, 1000
`main loop
do
`initiate jump
if yvel# > fall_speed_jump_limit# and yvel# =< 0 :`this means the player can only initiate a jump when the sphere has reached the top of its jump and begins to fall back down
if spacekey_pressed = 0 :`see comment below
if number_of_jumps < max_number_of_jumps :`limits the number of jumps that can be chained together
if spacekey() = 1
spacekey_pressed = 1 :`this flag stops the player from simply holding the spacekey down in order to chain jumps together
inc number_of_jumps :`this keeps a tally of the number of jumps the player has chained together
yvel# = jspd# :`the initial velocity of the jump
endif
endif
endif
if spacekey() = 0 then spacekey_pressed = 0 :`only reset the flag when the sphere as it the top of its jump and begins to fall back down
endif
`calculate the new veritcal position of the sphere and then adjust the y velocity for gravity
ypos# = ypos# + yvel#
yvel# = yvel# - grav#
`if the player has hit the ground (or the map)
if ypos# < rad#
number_of_jumps = 0 :`break the jump chain
ypos# = rad# :`set the position of the sphere the height of the ground plus its radius (ground here equals zero)
yvel# = 0.0 :`set the y velocity of the sphere to zero
endif
`position the sphere and point the camera at it
position object 1, 1000, ypos#, 1000
position camera 1000, 500, -500
point camera 1000, ypos#, 1000
set cursor 0,0
print "Press spacekey to make the sphere jump"
print "To do a double jump, release the spacekey after"
print "the first jump and press it again just"
print "as the sphere begins to fall back down"
sync
loop
end
Another jumping game play mechanic is one used in Canabalt where the player can do a quick tap for a low jump but then press and hold for higher jump, but only for a limited time.
One thing that you'll have to do is juggle around with thing like gravity and initial jump speeds (in Kezzla's code this would be gravity# and the 3 to -3 restriction on the force# variable) to get them to suit the scale of you game.