It gets horizontal input from the right and left arrow keys, in a way that right gets 1, and left gets -1
It multiplies those values with the move_speed
For vertical movement, i.e. jumping and falling, it does this:
It checks if there is ground below the player (by checking +2 pixels below it)
If there is ground, it sets move_y to 0 to reset the fall speed
Then it checks if space is pressed, and in that case, sets move_y to negative jump_speed so the player shoots up into the air
The final block runs if the player is not standing on ground, as it’s an else
It checks if move_y is below 10, and increases it – this is gravity
This won’t make the player move yet, for that we need to call move_and_collide().
In the same event, at the bottom (outside of any conditions) add this:
move_and_collide(move_x, move_y, obj_ground);
if (move_x != 0) image_xscale = sign(move_x);
This moves the player using our move_x and move_y values, and avoids obj_ground.
If move_x is not 0, meaning the player is walking, it sets the image_xscale to its sign (1 or -1). This means the sprite will flip to face left/right depending on where it’s walking.
Run the game, and you have a basic platformer!
Moving Down Slopes
Let’s fix moving down slopes:
For this, we’re gonna add code just before checking for the jump input, here:
We’ll check if there’s no ground where the player is going to move, but then we’ll check a few more pixels below, in case there is ground, just a bit lower than usual. This is how we’ll check for a downward slope.
When it finds a downward slope, it transfers the horizontal speed (move_x) into downward speed (move_y) – we know it’s downward as we use abs() to convert negative values to positive.
Then we set move_x to 0 so there is no horizontal movement.
This will cause the player to only move downward, hit the slope, and slide along it.
Limiting Movement
It can be useful to limit the player’s movement, to avoid it getting faster when hitting ground above or below you – this is due to its vertical velocity being directed towards its walking speed.
This can be avoided by specifying maximum speeds in move_and_collide(), which are the final two arguments of that function:
The first three new arguments (4, 0, 0) are optional, and we’re only specifying the default values so we can get to the last two arguments.
The last two arguments are the maximum amount that the player can move on the X and Y axes. I’m only passing move_speed for the X limit, so it doesn’t move faster than our defined walking speed.
For the Y limit, I’m passing in -1, so the Y speed is not limited.
With this, you no longer get the issue shown in the GIF above.
Shape Collisions
This function supports collisions for sprites of any shape – e.g. ellipse, diamond and precise shapes:
Using an ellipse shape for my player
Using a precise shape for this diagonal wall
Throw these shapes into the game and they will work perfectly:
Invisible Objects
If you’re primarily using tiles for your environment, you can still continue to use objects such as obj_ground for collisions. Just make the object invisible:
Place your ground instances in your room to cover your tiles. These will continue to work in the game, but won’t be visible to the player.