PLAYER COLLISION WITH TILES
First let's make sure the players don't fall through the tiles. Go to player 1's Object Properties and click the Add Event button near the bottom, then click Step and at the drop down menu click Step again. You have now added an empty step event. Whatever you put in a step event Game Maker will check every single frame.
Select the Step you just created and on the right side of the Object Properties window click the Control tab. Click and drag the Execute Code button from under the Code section into the empty area to the left of it. It is in this file we will write the code for player collision with the tiles and the player's basic movement.
Let's start with a simple if-else statement which defines the player's gravity.
if (place_free(x,y+1))
{
gravity = .2;
}
else
{
gravity = 0;
}
With this code we check if there is any free space (meaning there is no solid object, in our case a tile) at position x, y+1 from player1. The x coordinates we check are the same as the players x coordinates but we check one pixel below the players y coordinates, which is obvious because if a player would be standing on a tile it would be right below the player. Basically what we check here is if there is a tile right below the player or not.
If there is no tile (so there is place free), the player's gravity is set to 0.2. Else (meaning there is a tile/no place is free) gravity is set to 0. This means player1 doesn't fall.
Next add a Collision Event to the player1 object, then choose the tile object as the object with which it collides. Again drag the Execute Code button from the Control tab into the empty space an write the following code in it. This piece of code ensures you don't fall through the tiles.
move_contact_solid(270, vspeed)
gravity = 0;
vspeed = 0;
BASIC PLAYER CONTROLS
Now we're going to make sure we can move player1 with key presses. To do this we must first set the player's walking speed and jumping speed.
At player1's Object Properties click Add Event, then click Create. A Create Event is checked at the beginning of each Room, instead of every frame, like the Step Event. Now drag the Execute Code button into the empty area, just like you did before. In this piece of code we are going to define the variables for walking speed and jumping speed so we can refer to them later in different pieces of code. To do this write the following.
walk_speed = 3;
jump_speed = 3;
We have just created walk_speed and decided it's worth is 3, same goes for jump_speed.
Now go to the step event. We are going to make the player walk to the right.
if (keyboard_check(vk_right) && place_free(x+walk_speed,y))
{
x += walk_speed;
}
So basically what we do here is check if the right arrow key is pressed (keyboard_check) and if there is place free in the direction player1 is going to move (place_free). To check if theres place free we add the walk_speed to the player's x position, as the walk speed is how many pixels the player will be moving per frame. So if the right arrow key is pressed and there is space to move, we add walk_speed (which we defined in our Create Event as 3) to the player's x position every frame.
If you want to use a letter key instead of arrows or other special keys (shift, spacebar, ctrl etc), for example D, use the following piece of code isntead.
if (keyboard_check(ord('D')) && place_free(x+walk_speed,y))
{
x += walk_speed;
}
ASSIGNMENT: FINISH PLAYER MOVEMENT (LEFT, JUMPING)
Make sure player1 is able to move to the left with a key press and is able to jump with a key press. Use the jump_speed we defined in the Create Event for this and use keyboard_check_pressed instead of keyboard_check. This will make sure the player doesn't keep jumping when keeping the key pressed. Afterwards give player2 the same movement possibilities using other keys on the keyboard.
Tip: Use the following piece of code in the player's Step Event. Try to figure out what this code does.
if ((vspeed < 0) && !place_free(x,y+vspeed))
{
vspeed = 0;
}
Take a look at how I did it (including assignment answers) here.
Next add a Collision Event to the player1 object, then choose the tile object as the object with which it collides. Again drag the Execute Code button from the Control tab into the empty space an write the following code in it. This piece of code ensures you don't fall through the tiles.
move_contact_solid(270, vspeed)
gravity = 0;
vspeed = 0;
BASIC PLAYER CONTROLS
Now we're going to make sure we can move player1 with key presses. To do this we must first set the player's walking speed and jumping speed.
At player1's Object Properties click Add Event, then click Create. A Create Event is checked at the beginning of each Room, instead of every frame, like the Step Event. Now drag the Execute Code button into the empty area, just like you did before. In this piece of code we are going to define the variables for walking speed and jumping speed so we can refer to them later in different pieces of code. To do this write the following.
walk_speed = 3;
jump_speed = 3;
We have just created walk_speed and decided it's worth is 3, same goes for jump_speed.
Now go to the step event. We are going to make the player walk to the right.
if (keyboard_check(vk_right) && place_free(x+walk_speed,y))
{
x += walk_speed;
}
So basically what we do here is check if the right arrow key is pressed (keyboard_check) and if there is place free in the direction player1 is going to move (place_free). To check if theres place free we add the walk_speed to the player's x position, as the walk speed is how many pixels the player will be moving per frame. So if the right arrow key is pressed and there is space to move, we add walk_speed (which we defined in our Create Event as 3) to the player's x position every frame.
If you want to use a letter key instead of arrows or other special keys (shift, spacebar, ctrl etc), for example D, use the following piece of code isntead.
if (keyboard_check(ord('D')) && place_free(x+walk_speed,y))
{
x += walk_speed;
}
ASSIGNMENT: FINISH PLAYER MOVEMENT (LEFT, JUMPING)
Make sure player1 is able to move to the left with a key press and is able to jump with a key press. Use the jump_speed we defined in the Create Event for this and use keyboard_check_pressed instead of keyboard_check. This will make sure the player doesn't keep jumping when keeping the key pressed. Afterwards give player2 the same movement possibilities using other keys on the keyboard.
Tip: Use the following piece of code in the player's Step Event. Try to figure out what this code does.
if ((vspeed < 0) && !place_free(x,y+vspeed))
{
vspeed = 0;
}
Take a look at how I did it (including assignment answers) here.