As well as fixed platforms that are somehow suspended in the game world sky, and player characters that are subject to gravity (and are hence capable off falling of the edge of a suspended platform!), another defining characteristic of many arcade style platform games is the ability of a player character to jump.
Jumping is a core mechanic of many platform games, because it provides an easily understood way of helping a player character get from one platform level onto a higher one.
Before we look at jumping, however, let’s revisit how we go about making our player character move… To give it somewhere to explore, create a simple, platform filled room using instances of a solid, visible wall/platform object, configured with an appropriate sprite, but no event handlers. Also create a player object and give it a sensible sprite.
Moving the player character from side to side…
In a platform game, the player character typically moves only when an appropriate key is pressed – otherwise, it can be difficult to stop close to the edge of a particular platform.
One way of achieving this mechanic is to configure the player object so that it reacts to the keyboard arrow controls in the following way. When the appropriate keyboard event is raised, check to see if there is anything in the blocking the sprite’s movement in the desired direction:
Remember, the Game Maker co-ordinate system is defined with absolute co-ordinates (x, y) set to (0,0) in the top-left hand corner of the screen, increasing x from left to right, and increasing y from top to bottom. So to check the player character can move to the left, we need to check there’s nothing at the x coordinate location several pixels to the left of the character’s current position (that is, a negative x direction). Ticking the Relative box ensures that we are ticking 4 pixels to the left of the actual location of the player character, rather than the location at x=4.
If the path is clear, we want to move the sprite in the appropriate direction – use the
Jump to Position action to achieve this:
For the right arrow, the x value should be set to 4 in each case (that is, a positive x-direction, i.e. to the right). To get a less jerky movement, you might check closer distances/use a smaller jump distance in the arrow key actions.
Try your game out just to check that the player moves correctly in response to your keyboard commands.
In the official Game Maker platform game tutorial, it is suggested that gravity should only be “switched on” when an object is in mid-air – that is, when it unsupported by a solid platform block.
To test whether or not there is anything below an instance of an object, we will use a
Step event to repeatedly check whether there is anything immediately below the player character sprite:
If the player character is in mid-air, turn gravity on:
(The direction – 270 – is 270 degrees, measured in anti-clockwise direction from an origin of 0 degrees that points to the right. So 90 is straight up, and 180 is directed to the left…)
Otherwise, turn gravity off, by setting it to 0.
As the setup currently stands, there is no reason for the player character to stop moving through any platforms beneath it once it starts to fall. To stop the player falling through a platform, we need to set its vertical speed to zero when it collides with a platform block.
When we press the up arrow key, we want the player character to jump up. Can you work out how we might achieve this?
First of all, we need to ensure that the player is stood on something, so that they can generate the necessary force to lift itself off the ground… (physics again! Remember that ‘equal and opposite force’ law…?!). Check that there is something immediately below the layer sprite:
That is, we check that the there is NOT an empty space immediately below the player character (that is, at a y co-ordinate +1 relative to the current location of the bottom edge of the player character).
If the player character is a underneath a platform when it jumps, what happens? Can you explain why? Can you also explain why, when the player character jumps, it does not keep floatng up the room?
Experiment with various settings for the gravity in the room, the speed with which the player character jumps upwards, and the height and spacing between platforms). Can your player make it onto every platform? Try building a tall, vertical wall in the middle of the room and place platforms in such a way that the player character must bounce from one platform to the next to get over the dividing wall, and so cross the room.