Random Spawn without overlapping

Get help using Construct 2

Post » Tue Apr 16, 2013 10:07 pm

Hi, can anyone help me with this?

I have one sprite and i want to keep spawning the same sprite randomly on the screen, how could I avoid them spawning where it's another instance of the sprite?
I've tried to check collisions when the sprite is created, and if true move the sprite to a new random location, but it either moves both colliding sprites or does not move any.

Hope you can help me! :)
B
7
S
1
Posts: 14
Reputation: 1,178

Post » Tue Apr 16, 2013 10:29 pm

Maybe it will help you: just check the sprite colision and if it true - just destroy one of them.
B
55
S
19
G
8
Posts: 191
Reputation: 8,831

Post » Tue Apr 16, 2013 10:35 pm

I believe the reason your approach is not working is because it's 'picking' both sprites rather than just one. Here's how you might fix it. Let's assume your sprite is called 'Sprite'.

First create a family called 'SpawnedObjects' and add 'Sprite' to it.

Then, in your events, check to see if the newly created object 'Sprite' is overlapping any 'SpawnedObjects'. If it is, then position 'Sprite' to a new location.

This way, you've differentiated the created sprite from the already existing one. You've identified 'Sprite' as the newly created one and 'SpawnedObjects' as the existing one. Simply move 'Sprite' and it should work.

Also, if you're not already doing this, make sure it continues to re-position 'Sprite' until it finally finds a spot. Use a loop. Otherwise, it might re-position 'Sprite' once to avoid the overlap, but won't re-position it if it happens again.Dalal2013-04-16 22:40:20
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Tue Apr 16, 2013 11:23 pm

If necessary, because your sprites are spawning too fast, you'll need an array.

Better ask @R0J0hound to help ^^

https://dl.dropboxusercontent.com/u/47035927/Samples/spawn_area.capx
ImageImageImageImageImageImage
B
92
S
19
G
12
Posts: 1,187
Reputation: 18,261


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, anty21ro, Magistross, Megavolt and 19 guests