Hello again! In the previous article we created a basic platformer using GML (Game Maker Language). In this one we’re going to make enemies for our platformer game, which move around and can be defeated by the player by jumping on it. This article is mainly aimed at complete beginners.
Create a 32×32 sprite for our enemy, the same way we did for our player. Name it spr_enemy.
Create an object for it (obj_enemy). Our obj_enemy can move left and right, so inside it we will create a variable called state which can either be “left” or “right” and defines where the object will move.
A variable stores some value and can be checked in a condition. For example,
price = 350;
Now the variable price holds the value 350. We can check for it in any condition,
if (price==350) make_something_happen(now);
So basically, that’s how variables work.
So add the Create event and put this code into it:
state = choose(“left”, “right”);
choose() selects a random item out of those specified inside its brackets. So, state will be chosen randomly from either “left” or “right” at the object’s initiation.
Add the Step event and put this code:
if (state==”left”) hspeed = -2;
if (state==”right”) hspeed = 2;
The states can be either left or right, so according to this condition, if the state is “left”, the hspeed will be -2 and the enemy will move to the left. If not, and if the state is “right” (which it will obviously be, what other option does state have?) then the enemy will move right (because of hspeed = 2).
We will add some more code to the Step event.
if (!place_free(x + hspeed, y)) if (state==”left”) state = “right”; else state = “left”;
if (place_free(x + hspeed, y) and place_free(x + hspeed * 16, y + 16)) if (state==”left”) state = “right” else state = “left”;
Let me first explain the highlighted code. It checks if the state if “left”, then changes it to “right”. If not, which implies that the state is “right”, it will change it to “left”. So what this does is change the state so that the enemy changes its direction.
Now the conditions. The one condition in the first line checks for a solid object before the enemy, so it can turn away and not go through it.
The exclamation mark (!) before place_free() is the same as the keyword not we used in the previous article. It changes true to false and vice-versa in the condition.
The two conditions in the second line check for the floor edge. The first condition checks for empty space in front of the object and the second one checks for an empty space at the floor before it. So, it checks if the floor is ending. Without this condition, the enemy will start walking in midair, so with it the enemy changes direction when it comes to the floor’s edge.
You can place the enemies in the room and see them moving around. However, the player cannot yet interact with it.
Let’s make it so that if the player touches the enemy, the level (room) gets restarted, implying that the player lost, but if the player jumps on top of the enemy, the enemy object gets destroyed, implying that the enemy died.
Open your obj_enemy and add a Collision with obj_player event. Add this code in there:
if (other.y + 16 < y and other.vspeed > 0)
other.vspeed = -13;
other is used to reference the other object in the collision, which here is obj_player.
The two conditions check if the player is jumping on top of the enemy. The first condition checks if the player is above the enemy. The second condition checks if the player’s vertical speed is more than 0, which implies that it is falling down. When both conditions are true, the player destroys the enemy object and makes a jump.
If the conditions are not true, which means the player is not jumping on top of the enemy, the player loses and the room is restarted.
Now you can jump on your enemies and kill them, or lose if you touch them.
You can download this example here and import it into your GameMaker: Studio.
Need some help, or have any suggestions? Comment below, or contact me through Facebook or e-mail: email@example.com.
Have a great day!