How do I spawn within an area

Just started using Construct 2? Post your questions here

Post » Sat Feb 20, 2016 9:48 am

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) ?

Image
B
6
S
2
Posts: 34
Reputation: 512

Post » Sat Feb 20, 2016 10:02 am

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.
Mistakes were made.
B
52
S
26
G
111
Posts: 1,628
Reputation: 62,677

Post » Sat Feb 20, 2016 12:50 pm

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

Image
B
6
S
2
Posts: 34
Reputation: 512

Post » Sat Feb 20, 2016 5:45 pm

oosyrag wrote:
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?
B
6
S
2
Posts: 34
Reputation: 512

Post » Sat Feb 20, 2016 5:49 pm

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.
Mistakes were made.
B
52
S
26
G
111
Posts: 1,628
Reputation: 62,677

Post » Sun Feb 21, 2016 11:37 am

oosyrag wrote: ....


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!
B
6
S
2
Posts: 34
Reputation: 512


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests