Bruce

Nov 6, 20213 min

Let's create an MPAGD game: Part 28: Deep Dive into JUMPing

If you're designing a 2D side-on game, like a platformer, chances are your player will need to JUMP. Which is easily added by binding a JUMP command to a specific key press.

In our a game at the moment, we have a few lines of code in our Player Event that handles the jump:

IF KEY FIRE
 
JUMP 7
 
ENDIF

The '7' is the strength of the JUMP, we can change it to a different value if we want a higher or lower jump.

All pretty straightforward.

But what you might not realise is that MPAGD also has two additional player variables that we can work with to alter the way the Jump works, or what the player can or can't do when in the air. These are:

AIRBORNE: has a value of 0 when the player is on the ground and increases the longer the player is in the air

JUMPSPEED: holds a value which controls the rate of ascent/descent. We can tell in the code whether the player is ascending or descending.

Let's give this a test. First let's create a simple test screen. In your screen editor, create a screen with a set of wallblocks all the way round. Then be sure to add your player sprite using the SPrite Positions Editor.

Add your screen to the Map and hit enter to make it the starting screen.

Next, open up your Player Event code and add the following code above the FALL command:

IF KEY OPTION2
 
ADD 1 TO J
 
WHILE KEY OPTION2
 
ENDWHILE
 
IF J > 20
 
LET J = 0
 
ENDIF
 
ENDIF
 

 
AT 2 3
 
PRINT "JUMP:"
 
AT 2 13
 
DISPLAY TRIPLEDIGITS J
 

 
AT 3 3
 
PRINT "JUMPSPEED:"
 
AT 3 13
 
DISPLAY TRIPLEDIGITS JUMPSPEED
 

 
AT 4 3
 
PRINT "AIRBORNE:"
 
AT 4 13
 
DISPLAY TRIPLEDIGITS AIRBORNE
 

 
IF AIRBORNE <> 0
 
IF JUMPSPEED < 50
 
ADD 0 TO JUMPSPEED
 
ELSE
 
IF JUMPSPEED > 240
 
ADD 0 TO JUMPSPEED
 
ENDIF
 
ENDIF
 
ENDIF

Now run your game.

When it starts, tap on the '2' key. You should see the JUMP: value increase on the screen, set the jump to 10

Now press your jump key (space!)

Big Jump!

Keep jumping and notice how the numbers for each of the variables change.

As the player's jump ascends JUMPSPEED increases starting from 256 - JUMP STRENGTH, so if you use JUMP 10, JUMPSPEED starts from 256 - 10 = 246

Remember, this is an 8 bit number so when it reaches 255 and next increments it will return to 0. In terms of our JUMP, when JUMPSPEED reaches 0 the player reaches the peak of their jump and will start to descend. JUMPSPEED when descending will increment from 0 to the JUMP value, when it reaches (in our example) 10 the player will have landed.

AIRBORNE, on the other hand, simply increases for the time the player is in the air, starting from 0 and incrementing. When AIRBORNE is 0 the player is on the ground, for any other value we know the player is in the air. We can use this to our advantage by using a statement like IF AIRBORNE <> 0 (i.e. if the player is on the ground, do something), remember <> means 'IS NOT'

In terms of messing around with the JUMP, the code I gave you above is all set up to allow you to test what happens if we alter the rate at which JUMPSPEED changes:

IF AIRBORNE <> 0 ; player is in the air
 
IF JUMPSPEED < 50 ; is player descending?
 
ADD 0 TO JUMPSPEED ; if so change the rate of fall
 
ELSE
 
IF JUMPSPEED > 240 ; is player ascending
 
ADD 0 TO JUMPSPEED ; change the rate of ascent
 
ENDIF
 
ENDIF
 
ENDIF
 

By changing the 0 in the highlighted parts of the code you will alter the rate of ascent/descent

Give it a try, here's what happens when we change the rate of descent by:

ADD 5 TO JUMPSPEED

In this case the ascent is normal but the descent is much faster.

Note, that the standard MPAGD JUMP routine is a 'Mario' style jump, the player can move and change direction while in the air, it's not a fixed 'Manic Miner' type Jump.

Remember, if your game involves JUMPing, ensure to have a FALL line in your player event code, otherwise they may never come down!

    4131
    4