How do I spawn within an area

0 favourites
  • 6 posts
  • Can some of the skilled Construct 2 ninjas please advise me how to spawn 2 objects, at random points but on opposite positions relative to each other, only within the black area (as seen below) ?

  • I would treat the spawn area as 4 spawn rectangles. First ceil(random(4)) to pick which rectangle the first object spawns on, then create it (assuming origin point of the spawn rectangle is top left) at spawnrectangle.x+random(spawnrectangle. Width), spawnrectangle.y-random(spawnrectangle.Height)

    For the other object, use layout.Width-object1.x and layout.height-object1.y to get the opposite location. (Or whatever width and height the total black area is, if your layout is bigger.)

    This is slightly less than true random, because the corners will overlap and you have a higher chance of spawning there overall compared to the sides.

  • Thank you! You got me thinking in a different direction. Overlapping shouldnt be a problem if I do this:

  • For the other object, use layout.Width-object1.x and layout.height-object1.y to get the opposite location. (Or whatever width and height the total black area is, if your layout is bigger.)

    My second object keeps spawning in the bottom right of the screen using your suggested code. Any idea why?

  • Hmm not sure, hard to visualize. Sorry I was pretty tired when I wrote that, maybe it doesn't work.

    The idea is to take the difference between the area's total width or height, and where the first object spawned.

    Imagine 0 to 10 on a line, where 10 is your total width.

    Opposites would be 1 and 9, 4 and 6 ect. So if my object spawned at 1, 10-1=9. 10-4=6.

    If you do this for both the X and Y you should get the other side of a square. I think.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ....

    Sorry. I got it to work today. Had a piece of leftover code undoing what I was attempting ( hint : it involved "Pick furthest" lol lol ), but I got it to work now. Thank you so much for your help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)