How do I make this enemy spawner?

S

salsa3000

Guest
Hi GMC,

I'm new to GameMaker, and I'm making a space shooter game. I want to make an enemy spawn and respawn forever at a specific spot, and at a random time every 2 sec to 5 seconds. I have tried different youtube tutorials, and checked out forum posts, but I just can't make it work..

Any help would be greatly appreciated.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, you need a "controller" object to start with. This will be an object with no sprite or anything that is used to control different aspects of your game. In this case you want it to control the spawning of the enemy... Now in this controller you add a create event and in that set an alarm, like this:

Code:
// CREATE EVENT
alarm[0] = (room_speed * 2) + random(room_speed * 5);
So, here we set the alarm to run between 2 and 5 seconds after the controller is created (room_speed is the built in variable that holds the game steps per second, so if your game is 60fps, it means that 60 game steps will pass in one second... if we multiply this by two, we get two seconds, etc...). Next we need to add an alarm[0] event and in that have this:

Code:
// ALARM[0] EVENT
instance_create_layer(200, 300, "Instances", obj_Enemy); // Set this to the position, layer and instance that you require to spawn
alarm[0] = (room_speed * 2) + random(room_speed * 3);

Now, add this controller to your room and you will enemies spawn every 2-5 seconds infinitely... Oh, and don't forget to look up Alarms in the manual. :)
 

xNYARLx

Member
I want make this to random spawn but off the screen, to make them fly cool, how to make?
and when i make second respawn this work wonderful but i dont know what im making bad when i want giving this to my respawn script. My script works very vell but i want something like this to my game too...
I create ALARM in my spawning OBJECT and in CREATE im add this what u write... and dont work... This BOLD is yours.

----------------------------------------------------------------------------------------------------------

My [ALARM 0]--------------------
instance_create_layer(300, -40, "Instances", o_enemy_ship_three);
alarm[0] = (room_speed * 2) + random(room_speed * 5);

-----------------------------------------------------------------------------------------------------------

My o_enemy_spawner [OBJECT]------------------
var _enemy_number = instance_number(o_ship_parent) - 1;

if (_enemy_number == 0) {
var _enemies_to_spawn = 2 + score div 10;
spawn_enemies(_enemies_to_spawn, o_enemy_ship_one);

_enemies_to_spawn = 1 + score div 20;
spawn_enemies(_enemies_to_spawn, o_enemy_ship_two);
}
alarm[0] = (room_speed * 2) + random(room_speed * 5);

--------------------------------------------------------------------------------------------

My spawn_enemies [SCRIPT]--------------------
///@arg number
///@arg enemy
var _number = argument0;
var _enemy = argument1;

var _center_x = room_width/2;
var _center_y = room_height/2;

repeat(_number) {
var _direction = random(360);
var _distance = random_range(room_width * 0.6, room_width * 1.25);
var _x = _center_x + lengthdir_x(_distance, _direction);
var _y = _center_y + lengthdir_y(_distance, _direction);
instance_create_layer(_x, _y, "Instances", _enemy);
}

-------------------------------------------------------------------------------------------------
 
Last edited:
Top