Random monster spawn, but not on player screen

Get help using Construct 2

Post » Sun Feb 03, 2013 9:02 am

Hello!

(Sorry if I write badly, but English is not my native language.)

I keep on making a top-down shooter, and have stopped at one small problem I'm sure you guys can help me with. I am completely new to the Constructor 2 and use the free version.

What I'm trying to achieve is:

To spawn monsters total random all over the playfield, but not within the players sight (screen area).

I use this now, and this monster spawns all over the playfield, also within the player screen area.

Condition: System -> Every X seconds ->
Action: System -> Create object -> Monster, layer 1, Random (X), Random (Y).

Is it right that I intend to use the common condition "Is on screen" to fix this?

If anyone has the time to write down an example, or if you could point me in the right direction. I would really appreciate this!
B
6
S
1
Posts: 4
Reputation: 635

Post » Sun Feb 03, 2013 12:01 pm

@moffa

I'm not a math expert, so I think my expression is not so good, but you can try this. Find a X coordinate, then Y according to X:

Add a global variable ("randX").

Condition: System -> Every X seconds ->
   Action: Set randX to random(0,LayoutWidth)
   Action: System -> Create object -> Monster, layer 1, randX, randX<ViewportLeft(0) | randX>ViewportRight(0) ? random(0,LayoutHeight) : choose( random(0,round(ViewportTop(0))), random(round(ViewportBottom(0)),LayoutHeight) ).

You may have to adjust viewport layer numbers, and rounds.

Look at the manual for more details (expressions, system expressions)PaulPoy2013-02-03 12:02:03
B
31
S
9
G
1
Posts: 45
Reputation: 4,492

Post » Mon Feb 04, 2013 8:13 pm

Thanks for your advise. I will try this out in some days!
B
6
S
1
Posts: 4
Reputation: 635

Post » Mon Feb 04, 2013 9:31 pm

If you've got your player behavior to stay inside the layout along with the scrollto behavior so that you never see outside of the layout, you can spawn the monsters outside of the layout (assuming you don't have your monster with the destroy outside of layout behavior).

If you use rand(-500,layoutwidth+500),rand (-500,layoutheight+500), then check to see if the monster's X is less than 0 OR greater than your layoutwidth and the Y is less than 0 OR greater than your layoutheight. If they aren't, then move them outside of the layout and let 'em go.

If you wanted to get even fancier you could do something like this:

Demo
capx (R117)
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Feb 04, 2013 9:32 pm

if that doesn't work try adding a condition to the monster

monster - on created > set position Random (X), Random (Y)
is on screen

the idea is to check if it's on screen when it is created and if so set it's position elsewhere.
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Mon Feb 04, 2013 10:00 pm

@wretchedshark Only problem with that setup is if the random(x), random(y) put the monster inside the screen again. That event only will fire once when the monster is created.

Now, if you were to put a while & monster is on screen as a sub even there and set the position to random again that would work.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Tue Feb 05, 2013 7:56 am

edit: mistakePaulPoy2013-02-05 08:23:51
B
31
S
9
G
1
Posts: 45
Reputation: 4,492

Post » Tue Feb 05, 2013 8:46 am

Well this is handy i was just about to implant this into my game but now i don't have to figure it out myself. Thanks!
B
12
S
2
G
3
Posts: 59
Reputation: 2,628

Post » Tue Feb 05, 2013 11:44 am

@theubie @wretchedshark

Thank you guys! This helps me a lot.
B
6
S
1
Posts: 4
Reputation: 635

Post » Tue Feb 05, 2013 11:58 am

Just had a thought that might work and would take much less events.

I have some planets that orbit a star. I wanted them to have a random starting position at the start of every game so i made a global variable which is set at the start of the layout to random(360). The planets take their stating position from the global variable. If you would create a radius outside the layout size they would spawn at a random location outside of the layout size. Of course an enemy that spawns at 45 degrees would enter the layout sooner then an enemy that spawns at 90 degrees but as long that doesn't matter it should work.

global variables
radius = number bigger then the layout
randomspawn = random(360) you can set this at on every tick

spawn X and Y
sun.X+cos(randomspawn)*radius
sun.y+sin(randomspawn)*radius

Edit: Sun is obviously the center of the layout

Example
https://dl.dropbox.com/u/139739932/test/randomspawn.capxSemo2013-02-05 12:46:32
B
12
S
2
G
3
Posts: 59
Reputation: 2,628

Next

Return to How do I....?

Who is online

Users browsing this forum: Naightwolf, Skebab and 21 guests