• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Spawner inside walls

J

jewnko

Guest
Hey,

I'm trying to make a small 2D shooting game with the GM2.
I made a spawner for the monsters & tried making it so that the spawner doesn't alow the monsters to spawn outside or in the walls but it isn't working.

I used this code on the spawner

Create spawner:
Code:
spawnrate=60;
alarm[0] = spawnrate;
Step:
Code:
var xx,yy

xx = random_range(obj_Wall.x,room_width-obj_Wall.x-5);
yy = random_range(obj_Wall.y,room_height-obj_Wall.y+5);

instance_create_layer(xx,yy,"EnemyLayer",obj_EnemySpawn);
alarm[0] = spawnrate;
Thanks in advance!
 
M

moogthedog

Guest
Dependent on your origin point in your objects, you may need to shift the range slightly...

This example should work if the origin is in the top left for all the objects. I've spread out the code to make it more readable. It assumes the wall goes around the outside of the room, and is made up of obj_Wall 'tiles'.

Code:
// get the dimensions of all the objects involved
var _wallWidth=obj_Wall.sprite_width;
var _wallHeight=obj_Wall.sprite_height;
var _spawnerWidth=obj_EnemySpawn.sprite_width;
var _spawnerHeight=obj_EnemySpawn.sprite_height;

// allow for the wall width on the left and the additional space needed by the spawner on the right
xx=random_range(_wallWidth,room.width-_wallWidth-_spawnerWidth);

// do the same, but top->down rather than right to left
yy=random_range(_wallHeight,room_height-_wallHeight-_spawnerHeight);
 
Last edited by a moderator:
Top