Strawbry_jam
Member
GM Version: GameMaker: Studio
Target Platform: All
Downloads: Part 1 GMX, Part 2 GMX
Summary:
This tutorial covers a very simple way to create a platformer using Game Maker. There are several parts I will be covering going from basic to more advanced though each part can be considered its own platformer engine.
Tutorial:
Part 1:
This is a very generic platformer with slopes. The basic idea behind the movement in these platformers is the player first tries to move directly to where it is supposed to go, if that fails, we need to correct the problem.
There are many built in variables included with instances that automatically handle movement and collisions. Here is a list. The main variables that I use will be hspeed, vspeed, and gravity for the player. The walls will use a variable called solid.
hspeed adds to the x value of an instance every step. vspeed adds to the y value every step. gravity adds to the vspeed every step (assuming you don't change gravity_direction). If you set a collision event with an object that is solid and that event is triggered, the instance will move back to where it was before it moved in the step event.
So now we have the background down. I'll just post all the code for the player object:
Player Step Event
Player End Step Event
Player Collision with Wall Event
So as you can see, the code is heavily commented. I'll tell you what's going on any way. In the step event, we get the player's input and use that to set hspeed and vspeed. gravity happens when we aren't on ground and stops when we are. We can jump when we are on the ground. Going back to before, if we can't move to ( x + hspeed , y + vspeed ) without colliding with a solid, we stay at ( x , y ). That's where the end step event "corrects" this problem.
With the end step, the magic kicks in. We are moving up, up, over, down, possibly down, and down again. This method is the way it is because logically it makes sense. Players typically are trying to jump on top of platforms and are rarely trying to jump under a platform (see Figure A). If we're jumping, we want to move up first. To handle slopes, we move up over down and possibly down to stick to downward slopes. We have to move like this because slopes aren't the illusion we try to sell. They aren't actually flat. With square pixels, slopes are more like stairs (see Figure B). Then if we are falling, we will move down last for that. Finally we set our speeds to 0 when we are blocked. This makes the controls snappy and feel more natural.
Figure A
Figure B
There isn't any code for the wall objects. They just need to exist and be marked solid.
That's really all there is to it. Next part I will cover jump through platforms and slopes.
Here is a GMX of part 1. It has everything covered in this first part.
Target Platform: All
Downloads: Part 1 GMX, Part 2 GMX
Summary:
This tutorial covers a very simple way to create a platformer using Game Maker. There are several parts I will be covering going from basic to more advanced though each part can be considered its own platformer engine.
Tutorial:
Part 1:
This is a very generic platformer with slopes. The basic idea behind the movement in these platformers is the player first tries to move directly to where it is supposed to go, if that fails, we need to correct the problem.
There are many built in variables included with instances that automatically handle movement and collisions. Here is a list. The main variables that I use will be hspeed, vspeed, and gravity for the player. The walls will use a variable called solid.
hspeed adds to the x value of an instance every step. vspeed adds to the y value every step. gravity adds to the vspeed every step (assuming you don't change gravity_direction). If you set a collision event with an object that is solid and that event is triggered, the instance will move back to where it was before it moved in the step event.
So now we have the background down. I'll just post all the code for the player object:
Player Step Event
Code:
///standard platform movement
//controls for movement
hkey = keyboard_check( vk_right ) - keyboard_check( vk_left );
//check if on ground
if( place_free( x, y+1 )){
gravity = 0.8; //set gravity - we are in the air!
} else {
gravity = 0; //no need for gravity on ground
//we are on ground so we can check if we need to jump
if( keyboard_check_pressed( vk_up )){
vspeed = -12;
}
}
//set horizontal movement based on controls
if( hkey == 0 ){
hspeed *= 0.75; //friction
if( abs( hspeed ) < 0.5 ){ hspeed = 0; }
} else {
hspeed *= 0.75;
hspeed += 2 * sign( hkey );
}
Code:
///Collision Correction
//Moving into a solid puts us back as if we never moved.
//This part allows us to move on slopes and fill the gaps.
//First check if we need to correct for collision
if( speed != 0 and x == xprevious and y == yprevious ){
//move up first if vspeed is up
if( vspeed < 0 ){
move_contact_solid( 90, -vspeed );
}
//move over if hspeed is not 0
if( hspeed != 0 ){
//setting some temporary vars
var temp_y = y;
//move up
move_contact_solid( 90, abs( hspeed ));
//move over
move_contact_solid( 90 - sign( hspeed ) * 90, abs( hspeed ));
//move down what we moved up
if(temp_y-y != 0){
move_contact_solid( 270, temp_y-y);
}
//move down again if it puts us on ground
if( !place_free( x, y + abs( hspeed ) + 1 )){
move_contact_solid( 270, abs( hspeed ));
}
}
//move down if vspeed is down
if( vspeed > 0 ){
move_contact_solid( 270, vspeed );
}
//if we are blocked then set vspeed to 0
if( !place_free( x, y + sign( vspeed ))){
vspeed = 0;
}
//set hspeed to 0 if we can't move horizontally (we're blocked)
if( hspeed != 0 and x == xprevious ){
hspeed = 0;
}
}
Code:
///empty script to trigger collision event
//we don't need to do anything!
With the end step, the magic kicks in. We are moving up, up, over, down, possibly down, and down again. This method is the way it is because logically it makes sense. Players typically are trying to jump on top of platforms and are rarely trying to jump under a platform (see Figure A). If we're jumping, we want to move up first. To handle slopes, we move up over down and possibly down to stick to downward slopes. We have to move like this because slopes aren't the illusion we try to sell. They aren't actually flat. With square pixels, slopes are more like stairs (see Figure B). Then if we are falling, we will move down last for that. Finally we set our speeds to 0 when we are blocked. This makes the controls snappy and feel more natural.
Figure A
Figure B
There isn't any code for the wall objects. They just need to exist and be marked solid.
That's really all there is to it. Next part I will cover jump through platforms and slopes.
Here is a GMX of part 1. It has everything covered in this first part.
Last edited: