Tutorial 9: Spawning Enemies in GameMaker
First, what does spawning mean? Spawning refers to a method in game design where your enemies automatically get created
You will want to make your enemies appear at random in a top-down or side-scrolling shooter game, so that they do not become predictable every time the game is played.
Placing hundreds of enemies in a room will not work. Instead, you want enemies to randomly generate themselves. For this, you will need to make use of alarm events.
Create an enemy spawner object
-
- Create an object called objEnemySpawner – this object will have no associated sprite
- Add a Create event, inside the event type this code:
maxEnemies = 10; // variable to limit the amount of enemies in the room at once spawnRateLow = 20; spawnRateHigh = 80; alarm[0]=120; //trigger alarm 0 for the first time 2 seconds into the game //set this to give your player a chance before enemies appear
- Create an Alarm 0 event on the spawner object with this code:
if(instance_number(objEnemy1) < maxEnemies){ instance_create_layer(irandom_range(32,928), irandom_range(32,298), layer, objEnemy1); //spawn your enemy at a random point on the x-axis, between 32 &928 // random point on the y-axis between 32 and 298 //change these values to match the size of your enemy sprite } alarm[0] = irandom_range(spawnRateLow, spawnRateHigh); //alarm will now re-trigger between a the low and high values set in the Create event
- Drag the objEnemySpawner object into the room