I have a layer with 10 image-points and a family with 3 objects. I'm trying to spawn the objects randomly over the image-points randomly. The spawned objects are not moving but can be destroyed by another action. This works alright, but I am not able to prevent an object spawning over another one already spawned. If there is already one object spawned by an imagepoint, no other object should be spawned at that point.
Can anyone help me out? Thank
When I was making my rogue-like logic where it would generate a level, I did this by simply detecting if there is an overlap, destroy it, try and spawn it again. Another way might be to use your image-points as an object, give it a variable 'isUsed', set this to true when it is picked, then in the initial spawn logic make sure you pick an image-point that is 'isUsed=false'.
Hope my example gives you an idea for non repeating chosen image point:
https://www.dropbox.com/s/gkmisirvgerai ... .capx?dl=0
Thank you guys.
alextro: Puuh, pretty hard stuff for me. Sorry, but I don't get the code right now. But I'll keep on trying
plinkie: If I destroy the object immediately after spawning (in the case it overlapps an already spawned object) it destroys the object which is already there too. Which is not what i'm going for.
If I substitute the imagepoints by objects (spawner-sprites) and give them a variable, how do I address them in the random-spawn event? Right now, I'm working with:
system > every x seconds > Spawn-Object (the one with the image-points) > spawn family-objects (imagepoint random(1,10))
Yes it gets tricky when you are trying detect which imagepoints are used but without using an overlap. You could literally set an instance var for each imagepoint I suppose and when it has been picked, then it is set to used. Then you would need checks such as 4 is picked, is 4=used, pick another imagepoint if 4=used.
Regarding the overlap checks, yes it will destroy both as you need to pick one of the objects in the collision. You can use a comparision nth instance in overlap checks. When an object overlaps an object, pick nth instance 0 is one of the objects, pick nth instance 1 is the other object. This is how you can differentiate between the two overlapping objects and destroy only the new one.
To use actual spawn points rather than a single object with imagepoints, you set the spawn points up with variable. Pick a random spawnpoint where the instance var is not set, then spawn the object.
I will try my best with the spawn-objects rather than the image-points.
I set up the objects and instance var. But how do I pick a rondom object which instance var is set or not in the event sheet?
It's the system condition, pick a random instance, then pick the spawner. Then add another condition spawner.variable=false.
This picks a random spawner where the variable is false. It will ignore all spawners where the variable is true (already has a spawned object). When you spawn the object, then set the variable=true, it will use the same picked spawner and adjust the variable to say it is now in use.
Develop games in your browser. Powerful, performant & highly capable.
Yes Sir! The whole time I worked with 10 different spawner-objects. Now, I'm working with 10 instances of one and the same spawner-object and coded as you advised. And...it works. Thanks a lot, really appreciate your help and your patience!
No problem, good luck!
It's me again.
I'm struggling with setting the isused variable back to false, so when the object above it is destroyed, a new one can spawn. Checking, if the object is not overlapping the spawner is not working. Can you or someone else help me out one more time?
The easiest way is to have sprites as spawners instead of a layer with image points and just place these invisible spawners where you want them and do the following:
Every X seconds
---spawner is NOT overlapping spawned family
---pick random spawner
------------------------------------------------------------------------------------------> spawn family object
That only picks one of the spawners that is not overlapping a sprite from your family. No need for variables or anything complicated. A simple yet effective method And if the spawned sprite is destroyed later the spawner will be able to spawn a new one again.
Around where the destroy action is, you can say something like object is overlapping spawner, set variable to false, then destroy object. So really you are checking that is IS overlapping the spawner to pick the appropriate spawner, setting the variable back, then destroying the object.
https://www.dropbox.com/s/cjve1tc5x0zd6 ... .capx?dl=0
Thank you guys. Around the destroy action I set the variable back to false. And that does the trick. Had that before, but the order between the action wasn't right. Now it is. Man, this business is tricky sometimes