Enemy Spawn Coordinates - please help

i have enemies being created at coordinates X and Y. the X coordinate is set to a random interger between -1165 and 2535. The Y coordinate is set to a random interger between -1466 and 2234. how would i make it so those ranges wouldn’t include a 300 radius around the player sprite’s current center X and center Y coordinates? I’ve made several attempts to figure out a way to do this in the random interger equations but math and I have never been friends and im thinking that I’m going about this the wrong way. While it’s not a terribly frequent occurence, it is currently possible that an enemy spawns close enough to the player that it kills him instantly. Sorry if this has been discussed. I couldn’t find anything specific to this issue. Thank you

I can think of two ways, and I am sure others will have better suggestions, but, you could first when the enemy spawn delete it or move it by checking if within 300 px of the player (may not work if it spawns right on top of player and has a kill on collision)

Second option:
Store the random y and random x as a variable. Check that that variable is not within 300 ± of the player before spawning the enemy. So like scene var randy= RandomInRange(-1400,2200)
While Var(randy) <player.Y()+300 AND Var(randy) >player.Y() OR If Var(randy) >player.Y()-300 AND Var(randy) <player.Y() then scene var randy= RandomInRange(-1400,2200) else create the enemy.

Those are some possibilities to start working from, hope it helps a bit :slight_smile:


Thank you!! I dont know why i was thinking it had to be done directly in the create object line. I like that second approach a lot.

1 Like