How do I trigger individual spawner while player is in range

0 favourites
  • 11 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • Hi guys,

    I've got a problem with spawning enemies while multiple spawners are in player's range.

    I want to spawn one enemy whenever player gets near the spawner (in my example - less than 100px).

    Everything works fine when there is only one spawner within the player's 100px range (one enemy is properly spawned), but whenever more than one spawner is in player's range the enemies are spawning endlessly from all spawners except the first one.

    I've tried different approaches for measuring distance like:

    • System - distance(player.X, player.Y, spawner.X, spawner.Y) less than 100
    • above method, but with changing spawner's local variable "lod" to "1" when player is in range
    • using "line of sight" behaviour on spawners

    but all these methods produce the issue I mentioned earlier.

    I've tried also different ways to spawn enemies but without success

    • System - create object
    • spawner - spawn

    my events with all 3 methods of spawning

    and illustration of the issue

    It seems I'm missing something so any help would be much appreciated

  • Here are a couple of ways you can do it: ... .capx?dl=0

    In method 1 each spawner is constantly checking the distance to the avatar and updating its distance instance variable - if the avatar comes within range it spawns an enemy.

    In method 2 the avatar has a range sprite with a radius of 100, and the spawner spawns an enemy on collision.

    In both methods I've restricted the spawner to only spawning once by checking their "enemies" instance variable. You can increase this to spawn more enemies per spawner, or eliminate the the condition "spawner.enemies>0" to spawn an enemy every time the avatar comes withing range.

  • Thanks both of you

    tarek's method did the trick wonderfully

    here's an image if anyone else is intersted

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Personal, i am not that happy with that 'for each'.

    Just have to be aware of the different 'timings'. The animation changes when in/not LOS, the bullet fire happens once on approach. As you see, those are totally different 'timings'. ... .capx?dl=0

  • The animations you don't really need them it was more to give more juice to the example

    Anyway, The spawner it should be animation Speed 0 my mistake I did it quickly and I forgot to change that I didn't think you will keep the animations Frames lol

    Corrected Capx :

  • 99Instances2Go

    your capx is not really working like it should without the Each, at last here on my pc?? I didn't get what you mean about the (For each) problem

  • What did i miss ? (not the first and not the last time that i did something wrong)

  • What did i miss ? (not the first and not the last time that i did something wrong)


    I meant for this >>(Personal, i am not that happy with that 'for each'.)

    If I understood properly you said that you have a problem with the "for each" right? so because I'm not a Pro I'm just a beginner and I may have done something wrong so I would like to correct it if possible, every day we learn better ways to do things in it, that is the fun part.

    so I look up in your capx to learn the correct way of doing it without the "for each", but I see like the spawners don't spawn individually when the player is on the range. maybe I'm wrong but shouldn't each spawner spawn once when the player is in range, basically what the op was asking if I'm not mistaking please correct me if I'm wrong

  • Ahhhhh, you are right, that capx was close to rubbish. Allow me to make a correction. ... .capx?dl=0

    About THAT 'for each'. That is a loop, running trough all (picked) instances.

    Personal i do not like a 'for each' as 'first' condition, because it runs trough all instances.

    With a lot of instances, that is gonna hurt performance.

    Worse, I have seen a lot of projects with several 'for each' loops running trough the same instances.

    I try to avoid that.

    I have no problem with a 'for each' after pre-picking conditions. Like this:

    Player has LOS to Spawners

    For each Spawners <---- runs only trough those that are in range (picked)

    You see the difference ?

    Not that i am that bright to always know how to avoid such a root 'for each'.

  • 99Instances2Go

    Nice one!!

    I see what you mean now, you are totally right that's the most efficient and elegant way of doing it, I keep forgetting that the "for each" picks all the instances and it should be after the picking, I will keep it in mind for the future

    Thanks a lot for the reminder and the Tips

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)