Enemy random position onscreen but not near player?

JCFordJCFord Member Posts: 785
edited November -1 in Working with GS (Mac)
I have enemies that I place at random positions onscreen at the start of a scene using:

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?


  • SparkyidrSparkyidr Member Posts: 2,033
    I did this in my avoid em up game

    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.
  • JCFordJCFord Member Posts: 785
    thanks Sparkyidr but not quite what I want as I need the enemies all around the screen, but just need to try and keep them clear of my player when they spawn into their random position.
  • BeyondtheTechBeyondtheTech Member Posts: 809
    Here's what I would do.

    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.
  • JCFordJCFord Member Posts: 785
    Very nice BeyondtheTech, like the solution, I will try that later, but at the moment I have an issue that since the 0.8.x update I cannot get anything to SPAWN?
  • BeyondtheTechBeyondtheTech Member Posts: 809
    Unfortunately, I'm at work and don't have access to a Mac or the GS Creator until later tonight. Using 0.8.2?
Sign In or Register to comment.