In this tutorial you will learn how to make platform game engine that uses gravity and jumping.
Make sure you have the following:
Create the Wall Object
Note: If you already have this object, skip this step.
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 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.
Now you have a variable for the speed, you can add the code to move the character left and right.
The first action to add is an IF statement that checks the place to move into is empty.
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.
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.
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.
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.
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.
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,
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.
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
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.
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.
You can change the Vert Speed value to what you want, this is basically your jump height.
For those that don't want to read about what the actions do and why, a step by step summary.