Randomly spawn sprite out of range of target

Get help using Construct 2

Post » Tue Sep 17, 2013 12:14 am

hi,

anyone have a good idea how to randomly spawn a sprite with turret behavior..but make sure when it spawns it's not in range of the target?

since the target is moving I assume you need to do some sort of calculation based on target.x and target.y

I also have wrap behavior for both turret sprite and the target which both move...

B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Tue Sep 17, 2013 11:57 am

Just ensure that it's always spawned target.x(+range.x),target.y(+range.y) away from the target? Is the range always the same distance?
B
47
S
17
G
81
Posts: 2,223
Reputation: 48,093

Post » Tue Sep 17, 2013 3:49 pm

yes, the range is static. (200 let's say)

but my layout is 800x800 and if the Target happens to be at pos 780,780 then I can't say Create Object (Turret, Target.x+300, Target.y+300) because it will spawn off screen.

I need it to calculate going back to zero after the position goes past the layout.x and layout.y
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Tue Sep 17, 2013 4:29 pm

The way I would do it.

Spawn the object off screen. The object would have variables prototypeX and prototypeY. You would perform your placement calculations on those variables and check if they would be acceptable(on screen and out of range of the target).

If the prototype variables are acceptable then set turret.x and turret.y to the prototype variables. If not redo the placement calculations on the prototype variables until you have acceptable values.

You might have a system to note why the values were unacceptable and seed the recalculation accordingly.


You could bypass the prototype variables and just move the turret around until placement is correct, but on the off chance the game slowed down during that moment, the moving turret might be seen.

B
26
S
8
G
2
Posts: 33
Reputation: 3,645

Post » Tue Sep 17, 2013 8:24 pm

You could also pin an invisible object in front of your player. At the start of layout SpawnerObject.X = Player.X + 300 (or whatever the distance is you want it to be)

SpawnerObject Pin to Player

Now the spawner was created the correct number of pixels in front of the player and will stay in that position. You then have your random enemies spawn at the .X position of the spawner object. This ensures they will always be the correct distance from the player. You can then set their Y coordinates to whatever you want (random, or on the floor/ceiling, etc...).


B
49
S
12
G
10
Posts: 1,833
Reputation: 14,603

Post » Tue Sep 17, 2013 8:41 pm

[QUOTE=BluePhaze] You can then set their Y coordinates to whatever you want (random, or on the floor/ceiling, etc...).


[/QUOTE]

thanks for the reply

the only problem with this is my player rotates, so both player coordinates have to be modified.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Tue Sep 17, 2013 9:17 pm

[QUOTE=aerirprown] If not redo the placement calculations on the prototype variables until you have acceptable values.

[/QUOTE]

thanks for the reply..

so you are saying to create a loop and keep checking until you have acceptable parameters?

but what about the calculation?

player.x + turret.range + random(layout.width)
player.y + turret.range + random(layout.height)

say my layout is 800x800
and range is 300
and the player is at 700,750

the generated values would have to be approx between 0-400,0-450

if the player is at pos 20,100 then of course the regular calculation would work...

I probably have to do a bunch of checking I guess...like:
if player x is > (layout.width - range) then subtract range instead of adding range etc...

just wondering if there's an easier way to do it.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Wed Sep 18, 2013 2:43 am

I would do it something like this.

Function placeTurret
Set prototypeX to random(layout.width)
Set prototypeY to random(layout.height)

If distance(prototypeX, prototypeY, player.x, player.y) <= turret.range
------->call placeturret

else if prototype variables are outside layout
------->call placeTurret

else set turret x/y to prototypeX/Y

This way the function calls itself until an acceptable value is found. You would initiate it by calling this function upon creating the turret.
B
26
S
8
G
2
Posts: 33
Reputation: 3,645

Post » Fri Sep 20, 2013 1:50 am

[QUOTE=aerirprown] I would do it something like this.

If distance(prototypeX, prototypeY, player.x, player.y) <= turret.range
[/QUOTE]

ahh using the 'distance' function.. I haven't used that before, cool thanks..

but about your: "If, else if, else"

I wasn't aware you could do "else ifs" and then another nested "else" from the same event.. I actually tried, but maybe I am doing something wrong? I thought it was a limitation of C2...
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests