GameMaker : Plateform Game part 1


Make a plateformer game with Game Maker or Game Maker Studio.



First of all we need to make the sprites :
-Player Idle.
-Player Runnig.
-Player Jumping.
-Ground
For the player sprites they should be looking to the right side and we don't need them for the left side.






All the sprite's origin should be in the center, to do it click at the center button:




 And now let's make 2 objects:
-The player. (With the player idle sprite)
-The ground.


Let's start with the ground object:
First we need to make it solid. That's all.

And now the player's turn :
We need to 3 events :
-Create event.
-Step event.
-Collision with the ground event.

Create Event :
Drag the execute code icon the the actions list, to start.


We will make a variable :
facing = 0; 

And then close and it's the collision event turn.
Collision Event :
Do the same as create event drag the execute code to the action list, and then:
Write the code:

 move_contact_solid(270, 1);
vspeed = 0;


If the player collide with the ground the first code will make the player move down : 270 degree by 1 pixel until it collide and then the second code : vspeed = 0 will make it stop.



Step Event 
Do the same as create event drag the execute code to the action list, and then:
Write the code:



if place_free(x,y+1){
    gravity = 0.5;
}else{
    gravity = 0;
}


This  code will make the player fall while there is nothing under the player by one pixel but if there is something hi will stop falling.

if (keyboard_check(vk_right)){
    sprite_index = sprPlayerRunnig;
    x+=4;
}

This will move the player by four pixels on the right if the right arrow key is pressed.

if (keyboard_check(vk_left)){
    sprite_index = sprPlayerRunnig;
    x-=4;
}


The same as the other but this to the left.
if (keyboard_check_pressed(vk_up)) && !place_free(x,y+4){
    sprite_index = sprPlayerJump;
    vspeed = -6;
}

If the player presse the up arrow key and there is no place free under the player he will jump by 6 pixel.


The sprite_index = will change the sprite of the player to the sprites we have made.

If you test the game there are some problemes :

-When moving left the sprite of player run at the right direction even when jumping.

-When moving or jumping the sprite change but when we stop the sprite don't change to it's origin : player idle.


 So let's fix these probmems:

In the create event we have made a variable : facing = 0
This variable stock the facing direction of the player
for exemple if he is moving right facing will be equal to 0, if the player is moving left facing will equal to 1.
With this variable we will fix the sprite probleme, just follow :

We will change :

if (keyboard_check(vk_right)){
    sprite_index = sprPlayerRunnig;
    x+=4;
}


to :

if (keyboard_check(vk_right)){
    if (facing == 1){
        facing = 0;
    }

    sprite_index = sprPlayerRunnig;
    x+=4;
}


And the same with the left, but we reverse the facing variable.


if (keyboard_check(vk_left)){
    if (facing == 0){
        facing = 1;
    }

    sprite_index = sprPlayerRunnig;
    x-=4;
}


And now there is a function that will fix the sprite probleme by using it the facing variable :

image_xscale *= -1;


So let's add it :

if (keyboard_check(vk_right)){
    if (facing == 1){
        facing = 0;
        image_xscale *= -1;
    }
    sprite_index = sprPlayerRunnig;
    x+=4;
}


if (keyboard_check(vk_left)){
    if (facing == 0){
        facing = 1;
        image_xscale *= -1;
    }
    sprite_index = sprPlayerRunnig;
    x-=4;
}




Now the probleme is fixed.



 Let's go to the second probleme :
It's very easy we will just need to add to code :

if (!keyboard_check(vk_anykey)){
    sprite_index = sprPlayerIdle;
}



And that's all for the part 1 hope you enjoy with this little tuto.



Comments