Moving platforms tutorial
A german translation of this article is made by Windapple and is available at the German GMC.
This tutorial describes how to create moving platforms, like elevators for a platform game. It will first describe moving platforms horizontal and after that vertical. This tutorial assumes you are already capable of creating a platform game. A tutorial on how to create a platform game can be found on the Game Maker Website.
Horizontal Platforms
With a horizontal moving platform the trick is to check the speed of the platform below you and adjust your speed to it. First you need to check whether you are standing on ground:
Example code |
if !place_free(x,y+1) then
{ gravity:=0; vspeed:=0; } |
Then you need to add a check whether you are standing on a moving platform:
Example code |
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
{ //some code } |
You see that I use for the x coordinate: x-sprite_xoffset+sprite_width/2. x-sprite_xoffset means that you start looking at the left side of the image. Even if the origin of the image is set to a different position. Then the sprite_width/2 means that the x coordinate moves than to the right with half the sprite's width. So the x coordinate will be at the middle of the image. For the y coordinate you see y-sprite_yoffset which means the coordinate of the top side of the sprite. When adding the sprite height to it it will become at the bottem site of the image. So there will be checked whether there is a moving platform in the middle of the character right below the character.
Now you only need to make the movement. For this you can set the hspeed of the player to the hspeed of the block:
Example code |
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
{ objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform) hspeed = objID.hspeed; } else { hspeed:=0; } |
This code will check whether there is a moving platform below the character and if there is, take is's ID and set the hspeed to the hspeed at that coordinate.
This will give you a horizontal moving platform.
A few notes:
- In the above code you need to fill in object_moving_platform, fill in on that place the name of the object of the moving platform. If you have multiple moving platforms with different settings then create one object, which you set as parent in all the moving platforms and to which you let the code point. This will make the code work with all the moving platforms
- Please remember to make the platform solid!!!
- An important detail is to make it so that you can not get stuck between the moving platform and the ground. So make that the moving platform will automatically switch direction when you collide with it and your y coordinate is larger than the y coordinate of the moving platform at which you need to take account of the height of the sprite as well:
Example code if y-sprite_yoffset+sprite_height>other.y-other.sprite_yoffset then
{
other.hspeed:=-other.hspeed;
} - There is an example file with horizontal and vertical moving platforms, get it here
Vertical Moving Platforms
Vertical moving platforms are a lot more difficult than horizontal moving platforms because there is always the risk of you getting stuck in them. The positive thing is that vertical moving platforms which are moving down are so that you also move down because of the gravity so they always work.
For a vertical platform you can use the same code as above with a few minor adjustments
Example code |
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
{ objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform) vspeed = objID.vspeed; } |
However using this code will give problems because when you are not yet standing on top of the platform you can get stuck inside it. So add:
Example code |
if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform) { objID = instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform) vspeed = objID.vspeed; move_outside_solid(90,sprite_height); } |
A few notes:
- Make sure that the moving platform never squashes you. So make sure the moving platform stops when approaching the roof on a distance of at least your character's sprite height. Else way your character gets stuck inside the roof.
- With a vertical moving platform you do not need to have a complex collision test for when the player approaches from the wrong side. Because then the player will simply drop through the moving platform and the platform will keep moving away from him so he is automatically free although he might fall down some distance.
- Please remember to make the platform solid!!!
- Please note that the unit can not jump when on the platform. In order to achieve this you need to add some more code:
Example code if position_meeting(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
{
objID=instance_position(x-sprite_xoffset+sprite_width/2,y-sprite_yoffset+sprite_height,object_moving_platform)
vspeed = objID.vspeed;
move_outside_solid(90,sprite_height);
if keyboard_check(vk_up) then
{
vspeed:=-10; //change 10 to the jump speed
}
} - There is an example file with horizontal and vertical moving platforms, get it here