Platformer Enemies (GML) [GameMaker: Studio]

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.

Enemy Object

Create a 32×32 sprite for our enemy, the same way we did for our player. Name it spr_enemy.

1.PNG

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;
else
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.

2.gif

Player interaction

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;
instance_destroy();
}
else
{
room_restart();
}

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.

Done!

Now you can jump on your enemies and kill them, or lose if you touch them.

3.gif

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: gurpreetsingh793@gmail.com.

Have a great day!

P.S.: Check out my GML course at 75% off: Udemy. The offer expires on 16 Oct 2016. If it’s not available, use this 70% off coupon.

Advertisements

2 thoughts on “Platformer Enemies (GML) [GameMaker: Studio]

  1. if (place_free(x + hspeed, y) and place_free(x + hspeed * 16, y + 16)) if (state==”left”) state = “right” else state = “left”;

    why sixteen ? may i ask ?

    Like

    1. –> place_free(x + hspeed * 16, y + 16)

      The first sixteen (in “x + hspeed * 16”) implies how far away the enemy object will check for a ledge (in pixels). If you change it to 32, you’ll see that the enemy turns away from ledges way earlier than it did with 16.

      The second sixteen (in “y + 16”) implies that the enemy will check for a floor (solid) object 16 pixels below it. Since in our project, the floor blocks are 32×32, 16 is the appropriate distance to check for a floor block. However, any value between 1 and 63 should work, because of the enemy object’s and the floor block’s heights. ((32 + 32) – 1)

      Hope this helps!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s