Featured Post

Dodgeball Game Maker Tutorial

This will be a series of posts that will teach you step by step how to make a simple arcade game using Game Maker Studio 2. The final game w...

Thursday, September 17, 2020

Dodgeball Tutorial: Enemy Spawner

On this section of the tutorial we will create an object that spawns enemies on the screen. This will be a logic object and it won't have a sprite, but it will be running code during the game and will be spawning enemies periodically, but first we will set the room layers to start having each thing in the right place.

Setting up room layers

On this section we will configure the room layers. For now we've been putting everything on the instances layer, but this is not the best thing to do it. We will create different layers for the different things that can be in our room.

To create a new layer, open the room editor and right click on the instances layer and select create new instances layer:

This way we can create different layers for our objects. We will create this layers and put them on this particular order: Interface, Player, Enemies, Items, Instances, Background. When we finish our layers should look like this:

Now we can delete our objects on the "Instances" layer and add them on they corresponding layer. The player under the player layer and the enemies under the enemies layer. This way we have things organized and it's a good practice to have rooms with layers because we can control what should be drawn on top.

Enemy Spawner Object

The first thing we need to do is to create a new object called oEnemySpawner under the objects folder, and create a create event for it.

On the create event we will define how fast enemies spawn and we will use a alarm event to make an enemy spawn. Alarm events are code that is executed when the alarm triggers and this is usefull for example to make an enemy spawn every 2 seconds for example.

So the create event should look like this:


spawn_rate = 120;
alarm[0] = spawn_rate;

What we are doing here is telling the engine that we will execute the code inside alarm 0 after 120 frames which is equal to 2 seconds, as our game is running at 60 frames per second by default.

Alarm0 event

Now we have to actually create the alarm0 event and put this code inside to actually spawn a random enemy.


//Choose a random enemy to spawn
var random_enemy = choose(oEnemyMedium, oEnemySmall, oEnemyBig, oEnemySpecial);

//Chose a random side to spawn the enemy
var random_side = random(1);

if random_side < 0.25 {
	//Spawn from top
	instance_create_layer(random(room_width), 0, "Enemies", random_enemy);
} 
else if random_side < 0.5 {
	//Spawn from bot
	instance_create_layer(random(room_width), room_height, "Enemies", random_enemy);	
} 
else if random_side < 0.75 {
	//Spawn from left
	instance_create_layer(0, random(room_height), "Enemies", random_enemy);
} 
else {
	//Spawn from right
	instance_create_layer(room_width, random(room_height), "Enemies", random_enemy);
}

//we reset the alarm becase we want to keep spawning enemies
alarm[0] = spawn_rate;

The choose() function accepts multiple arguments and will return one of them randomly, we do this to get a random enemy to spawn as we pass it our enemy objects. Then we choose a random side getting a number between 0 and 1 and checking it's result, being each side 25% possible. Finally we set the alarm again because we want to be spawning enemies all the time. If we don't reset the alarm, the alarm will only be triggered once.

As we can see, alarms are a great tool to execute code that runs periodically but has not to run each frame (as we have the step event for that). But alarms can be used in a lot of case scenarios and they are very easy to use.

Updating the game room

Now we have to put our enemy spawner object into the room and run the game. We can delete the old enemies that we have (to select them we have to select the correct layer first!), as they will be spawning now by code. So select the enemies layer and then remove each enemy. Now, with the instances layer selected, place the enemy spawner object into the room. It doesn't matter where you put it but I like to place them on the top left corner of the room, which is position (0, 0).

Now if we run the game, every two seconds an enemy will spawn in the room and if we wait a bit, soon we will have a room full of enemies that want to kill us!

On the next chapter we will create the game over menu.

No comments:

Post a Comment