How do I Create An Object Outside of the layout randomly?

Get help using Construct 2

Post » Wed Apr 23, 2014 12:53 am

How do I Create An Object Outside of the layout randomly? I tried the following:
Create spr_enemy at X: random(LayoutWidth) Y: random(LayoutHeight) but this would spawn objects inside the layout :( Help is greatly appreciated.
B
37
S
14
G
3
Posts: 263
Reputation: 4,270

Post » Wed Apr 23, 2014 1:52 am

Well to spawn on the edge of the layout you can do it with two events:
Code: Select all
system compare choose(0,1) = 0
--- create object at random(LayoutWidth), choose(0, LayoutHeight)
else
---create object at choose(0, LayoutWidth), random(LayoutHeight)


You can then modify that to spawn further away from the edge (say 100 pixels) with:
Code: Select all
system compare choose(0,1) = 0
--- create object at random(-100, LayoutWidth+100), choose(-100, LayoutHeight+100)
else
---create object at choose(-100, LayoutWidth+100), random(-100, LayoutHeight+100)


Both those however only spawn along the edge of a rectangle. To spawn in a random area outside the layout you can break up the areas outside into four rectangles in which you can create objects in the same way as inside the layout. Keep in mind you need to define how big an area to spawn in.

For example the whole area outside the layout up to 100 pixels away:
Code: Select all
global number region=0
every tick
--- set region to choose(0,1,2,3)
region = 0
--- create object at random(-100, LayoutWidth), random(-100, 0)
region = 1
---create object at random(100)+LayoutWidth, random(-100, LayoutHeight)
region = 2
--- create object at random(0, LayoutWidth+100), random(100)+ LayoutHeight
region = 3
--- create object at random(-100, 0), random(0, LayoutHeight+100)


The formulas should be correct, but I haven't tested it.
-cheers
B
92
S
32
G
110
Posts: 5,295
Reputation: 71,501

Post » Wed Apr 23, 2014 1:55 am

Create spr_enemy at X: random(LayoutWidth) Y: random(LayoutHeight)

You kinda answered your own question :D

The layout in topleft corner is (0,0) and the bottomright is (LayoutWidth, layoutHeight)

So every value outside of these are outside the layout.

(-100, layoutHeight) Would spawn an enemy on a random point on the left side outside the layout.
(layoutWidth + 100, layoutHeight) Would spawn an enemy on a random point on the Right side outside the layout.

And the same for top and bottom.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Wed Apr 23, 2014 6:15 pm

Thanks @R0J0Hound and @nimos100. You guys helped solve my problem.
B
37
S
14
G
3
Posts: 263
Reputation: 4,270


Return to How do I....?

Who is online

Users browsing this forum: Armench, Jackriduk, kiki4construct and 25 guests