Platform Movement

Written by Luke Harrison 2007

In this tutorial you will learn how to make platform game engine that uses gravity and jumping.


Summary

  1. Create the Wall Object
  2. Create the Character Object
  3. Add Movement to the Character Object
  4. Add Gravity to the Character Object
  5. Add Wall Collisions to the Character Object
  6. Add Jumping to the Character Object
  7. Full Summary

Download Example


Before you Start

Make sure you have the following:

  • Sprite for the Character
  • Sprite for the Wall
  • If you don't, use mine, character and wall

Create the Wall Object

Note: If you already have this object, skip this step.

  • Add a New Object
    • Call this object, o_wall

  • Set the Wall Object as Solid
    • In the Object Properties, tick the Solid Checkbox

  • Set the Sprite of the Wall Object
    • Make sure that the Precise Collision Detection is off
    • Make sure that the Bounding Box is set as full image
    • All done in Sprite Properties

Ticking the Solid Checkbox this lets the game know that things shouldn’t be able to pass through it.
Only tick the Solid Checkbox on Objects that act as walls or floors ect, not characters or pick ups.


Create the Character Object

Note: If you already have this object, skip this step.

  • Add a New Object
    • Call this object, o_character

  • Set the Sprite of the Character Object
    • Make sure that the Precise Collision Detection is off
    • Make sure that the Bounding Box is set as full image
    • All done in Sprite Properties

Add Movement to the Character Object

First, you need to add movement, because you’re going to be using gravity, you can’t use the Start Moving in Direction Action, instead, you have to use Jump to Position with an IF Statement.

When moving the character, before every movement, the code will check if the place you are moving into is empty, if it is, then and only then it will move into it. The code will not allow you to walk into a wall.

First, it’s recommended you set a Variable on the character for move_speed, the reason for setting a Variable is so you can change the speed of the character at any time in the game.

  • Add a Create Event to the Character Object
    • If you already have one, add the following actions to it

  • Add the, Set Variable Action from the Control tab
    • Set Variable as, move_speed, this is the name of the new variable
    • Set Value as, 5, this will be the speed the character can move

Now you have a variable for the speed, you can add the code to move the character left and right.

  • Add a New Event, Keyboard / Left,
    • If you already have this event, remove the Actions that move the character.

The first action to add is an IF statement that checks the place to move into is empty.

  • Add the, If Position is Collision Free Action, from the Control Tab.
    • Set X as, -move_speed
    • Leave Y as 0
    • Tick Relative

Because you want the character is going to move left, you want to check to the left of your character, this is done by checking a set amount of pixels negative on the x axis from where your character is. The amount of pixels we want to check is the speed the character will move, if you remember, we made a variable, move_speed, this is the amount of pixels we want to check.

What this Action does is checks if a specified position is empty.

After the IF Statement, you should put what Action you want to happen if the Statement is true. After an IF Statement, the code with do the next Action block ONLY if the Statement is true, the block after that it will do regardless. An IF Statement always does the next Action or Line of code after if the statement is true. You can increase the amount of Actions performed if the statement is true with the Start and End blocks but this is covered later.

  • Add the, Jump to Given Position Action.
    • Set X as, -move_speed
    • Leave Y as 0
    • Tick Relative

The Jump to Given Position Action. action moves the object to a set position, we put this AFTER the IF statement so it will only happen if the statement, “is this position empty?” is true, so your object moves into that position only when the new position is empty.

That’s it, repeat this for a Right Keyboard Event but this time, just put move_speed, you don’t need to put the minus before it this time because right on the x axis is positive.


Add Gravity to the Character Object

Now the character can move, test if you want, we have to add gravity so it will fall when in the air.

Gravity is a constant force that pulls objects to the ground, we want to simulate this in the game. We will IF statements, we will check if the object is in the air by check if the pixel below it is empty, if it is, then the object is in mid air and we apply gravity to it, if the pixel isn’t empty, then we stop the gravity.

Because we need to keep checking if the character is in the air, the Event you need to use for this is a Step Event, a Step Event runs every frame in your game, so say your game is 30 frames per second, the Step Event will run 30 times in one second.

  • Add a New Event, Step / Step, if this already exists, add the following actions to the top

  • Add the, If Position is Collision Free Action, from the Control Tab
    • Set Y as 1
    • Tick Relative

This Statement will now check if the position 1 pixel below your character is empty. This will determine if the character is in the air.

After this Statement, we want to set the gravity.

  • Add the, Set Gravity Action, from the Move Tab
    • Set Direction as, 270
    • Set Gravity as 0.5

You have to set the direction that the gravity will pull your character, down and the force of the gravity. In game maker, directions are in degrees. Starting from 0 (Right) and going anti-clockwise back round to zero. So, Down would be 270.

Because we want to stop gravity if the character is on the ground, you have to use an ELSE statement. Else statements go after the Action following an IF statement, the Action after an Else statement will run if the IF statement is NOT true, so if the character is NOT in the air, then the Else Action will run instead of the If Action.

  • Add the, Else Statement Action, from the Control Tab.

  • Add the, Set Gravity Action, from the Move Tab.
    • Leave Values as they are by Default.

On the Set Gravity Action, you don’t need to change any of the values since you are stopping gravity by changing the Gravity value to 0 which it is by default when added.


Add Wall Collisions to the Character Object

Now if you test your game, your character will fall to the ground but get pulled through the walls. This is because Gravity changes a variable on your object called, vspeed, this value is the speed the object will move on the y axis, if it isn’t 0, it will move. To solve this problem, when the character collides with the floor, the vspeed must be reset.

  • Open the Collision Event with the Wall Object, if there isn’t one, add it.

The Collision should already have one action in it, Move in Contact Direction, this Action pushes your character out of the other object if it gets stuck inside.

If this action isn’t in the Event,

  • Add the, Move in Contact Direction Action, from the Move Tab.
    • Set Direction as, direction
    • Set Maximum as, 12

The reason you put, direction, in the Direction Value is because, direction is a variable on your object which stores the direction is moving in. The Action uses this value to determine which way to push the object away from the other object. Now to reset the vspeed.

  • Add the, Set the Vertical Speed Action, from the Move Tab.
    • Set Vert Speed as, 0

Now the character will get pulled to the ground by gravity when in air and stay on the ground. Next, we want to give the character the ability to jump.


Add Jumping to the Character Object

  • Add a New Event, Key Press / Up

In the game, we only want to be able to jump if the character is on the ground, otherwise, there is nothing stopping the player jumping repeatedly in the air.

To check this, we use another IF Statement, similar to the last one we used but this one checks if the space isn’t empty. Again, we will check one pixel bellow the character.

  • Add the, If There is Collision at Position Action, from the Control Tab.
    • Set Y as, 1
    • Tick Relative

This sets the statement up so it is checking that there is a solid object one pixel below the character. Now we need to add the action to make the character jump, all we have to do is change the vspeed variable, this will instantly make the character start moving vertically, the gravity function will kick in bringing it back down to the ground.

  • Add the, Set the Vertical Speed Action, from the Move Tab.
    • Set Vert Speed as, -10

You can change the Vert Speed value to what you want, this is basically your jump height.


Summary

For those that don't want to read about what the actions do and why, a step by step summary.

  • Create a Wall Object
    • Set as Solid

  • Create a Character Object

  • Add a Create Event
    • Set Variable, move_speed as 5

  • Add a Collision Event with the Wall
    • Move in Contact Direction
    • Set Direction as direction and Maximum as 12
    • Set Vspeed, set vspeed as 0

  • Add a Key Left Event
    • Check Position Free, x =- move_speed, relative
    • Jump to Position, x = - move_speed, relative
  • Add a Key Right Event
    • Check If Position Free, x = move_speed, relative
    • Jump to Position, x = move_speed, relative

  • Add a Step Event
    • Check If Position Free, y = 1, relative
    • Set Gravity, direction = 270 and gravity = 0.5
    • Add Else Block
    • Set Gravity, direction = 270 and gravity = 0

  • Add Key Press Up Event
    • Check if Collision at Position, y = 1, relative
    • Set Vspeed, set vspeed as -10

  • Create a New Room
    • Put in afew Wall Objects
    • Put in the Character Object