Enemy random position onscreen but not near player?
JCFord
Member Posts: 785
I have enemies that I place at random positions onscreen at the start of a scene using:
CHANGE ATTRIBUTE
self.position X to random (0,480)
self.position Y to random (0,320)
But because the placement is random sometimes they appear on top of my main player.
Is there anyway I can make them appear in a random position but not when my player is?
CHANGE ATTRIBUTE
self.position X to random (0,480)
self.position Y to random (0,320)
But because the placement is random sometimes they appear on top of my main player.
Is there anyway I can make them appear in a random position but not when my player is?
Comments
I did it in a fairly simple way...only still feels random if your player is moving around a fair bit though
think of the screen as 4 segments. top left, top right, bottom left, bottom right.
First off, constain your player x and y to game variables (game.player.x and y, or some such)
So have 4 spawn rules that spawn your enemies in the opposite segment to your player
so as an example
if player is at top left, spawn bottom right
think of top left as (x=0 to 240 and y=160 to 320)
then spawn an enemy bottom right
think of bottom right as (x=240-480 and y=0 to 160)
then just make 4 rules that do the spawning dependant on your players x,y position.
Upon Spawning a new enemy Actor, I would not make it visible right away (Alpha = 0). It would also have an Actor Attribute called SpawningMode initially set to `true.` Allow it to Spawn anywhere in the game area.
Create a Rule: if SpawningMode is `true` then,
- Get position of player
- Use `magnitude` expression to determine distance between player Actor and enemy Actor
--- Create a Rule: if distance is too close (you set the value yourself, adjust to taste), randomize position of enemy Actor again
--- Otherwise: make enemy visible, then turn off SpawningMode (set to `false`)
So, to sum up, an enemy will spawn invisibly, but will constantly be shifting around the screen until it's in a safe distance away from the player, and then it will appear once it's ready. If the Collision check against the Player is in the enemy Actor, you should also test the SpawningMode flag to be `false,` too, otherwise the player won't know what hit him.
Hope that makes sense and helps out.