Tag: safe enemy spawning

Spawning objects outside of a safe zone in Game Maker

In Game Maker, it’s non-trivial to spawn a new instance, avoiding collisions with existing objects in the game. This can make it tricky to create games with “fair spawning” so that the player isn’t automatically killed when a new, randomly-placed instance springs into existence.

It isn’t terribly complicated to work around this problem, thankfully. Basically, the approach I take is, knowing the x,y coordinates of the instance or object that you want to want to avoid collision with, and the size of that object’s sprite, you can define a “safe zone” around the instance you wish to protect.

I’ve whipped up a quick demo in Game Maker 8, showing a technique which seems to work pretty well. This is a very simple illustration of the approach, and could be refined further to handle wrapping the safe zone around the edge of a room, or to handle enemies of randomly varying sizes. To make it easier to understand, I’ve actually created an object to represent the “SafeZone” . This is unnecessary, as the dimensions involved can simply be calculated on the fly, using the right GML.

Source .gmk.