How do I spawn specific numb of enemies randomly periodicall

0 favourites
From the Asset Store
Pixel Enemies for SHMUP consists of 38 enemy ship sprites to be used in your game.
  • So, what I want to do, is create a bunch of spawn points off map, and set for enemies to spawn randomly at this spawn points periodically. I also want to have a specific total number of spawns.


    I create 30 spawn points.

    I want 20 enemies to spawn, between these spawn points

    I want the enemies to spawn roughly within a 2 minute time frame

    So I want each spawn to lower a specific value, when that value reaches 0, the random spawning is turned off on all points.

    My goal is this helps future level creation easy, because all of the code is identical, and all I need to change is how many total enemies I want to spawn, so I just adjust that variable up to increase difficulty.

    A concern I have after doing this is, with a random function, will i sometimes have instances when I have a bulk of the spawns go off all at the same time? If so, is there a way to manage spawn frequency between all points to keep them within some sort of threshold. Like no more than x amount of units will spawn within y amount of seconds of each other... something of that nature.

  • You could make an array with the locations, then just make an event every 120 seconds with repeat 20 to spawn an object, use with random to select one of your spawn points.

  • Ram, that is helping me some, I'll mess around with it some more, but is there a way to add in a spawn limit for number of units spawned?

    Martin, I was trying to mess with arrays, also using the tutorial here on scirra's website, but they depict an actualy seeable object, but when I make an array, I do not actually see it. I think the array method, in reading how they work, may actually work great. Seems I can set an array on all 4 sides, and spawn from them at random. so I want to learn more about them.

    I will try to learn some by looking up some youtube video's, but I usually learn best by manipulating similar code to what I'm looking for, so may see if I can find some turorial that includes some base code for me to play with and learn off of.

  • Ok Ram, you def have me in the right direction and I'm almost there. I understand the functions a little more clearly now, but still have a problem translasting it. I tried to bring it over more or less exactly, but without the wave functionality you built in yours, since mine is going to be more of a level system rather than waves. When doing that, It wouldn't wait between spawns, and it is spawning them infinitely.

    I got into looking through tutorials of some of the functions, and I wonder would the repeat function be something that would work for me? In my mind, I want to take the system repeat function, tell it to repeat x amount of times, then I want it to spawn object with a random range delay, so I could do random (3, 5) and it would randomly spawn between every 3 to 5 seconds. Then lastly, I want to use your set frame option, to have it randomly choose which one to spawn from.

    End result, game randomly selects spawn point every 3-5 seconds, spawns a unit, and repeats that function x amount of times. I'm going to mess with it a little longer before I call it a night.

    Edit: So I messed around with it a bit longer, and I came up with something that to me logically should work perfect, but logic isnt winning here. Here is what I have

    Condition: Repeat 10 times

    Condition: Pick a random instance (spawn sprite selected)

    Action: System wait 5*loopindex

    Action: (Spawn sprite) spawn (Object sprite)

    What I expected was for it to spawn the object 10 times, once every 5 seconds at a random spawn. What I got was it just spawning an infinite amount of objects at every spawn. I also tried your for command that you used, and ended up with the same result. I think I'm close, just missing something small I'm sure.

  • remember with C2 it constantly loops through the code - it sounds like you just need to add a

    "while this "level" is true do..."

    "make this "level" no longer true"

    Condition: Repeat 10 times

    Condition: Pick a random instance (spawn sprite selected)

    Action: System wait 5*loopindex

    Action: (Spawn sprite) spawn (Object sprite)

    so in my case I was using "gvWaveDelayCoundtdown" - this prevents the for/next loop running multiple times ...

  • RAM! Thanks sir. That last bit worked perfect. I want to make sure I have a grasp on why, so I can help myself more going forward, because it took me a few tries to add the items myself (I didnt want to just copy your code and change the values, and I'm glad I didnt since I made some mistakes I learned form along the way.

    1) The create object line in the beginning, true, to false, makes it so it doesn't infinitely run the routine, and rather just runs 1 instance of it.

    2) Making it a sub event, rather than a complete list of separate events also accomplishes that

    3) Loopindex makes it so the asteroids actually spawn 1 at a time, up to the value listed, rather than all firing off at the same time, at the value listed

    Does that sound about right?

    1 last question, doesn't look like I gcan make a global value random(x,y) correct? It always sets value to 0. So if I want to have a little variance in my spawn time, I'll just have to ditch the GV, and put in the random(x,y) code with the loopindex?

  • " I'm glad I didnt since I made some mistakes I learned form along the way" - best way to learn in my opinion.

    1- Yes -but , as always, there are other ways to do this

    2 - Not in this case , but it did make it easier to understand <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile"> - if you include the sub-conditions (in the same order) in with the main condition and also move the actions into the same block then it should still work :I've not checked this , please try it and see...

    3 - the loopindex is the number of time it steps through the loop so the wait loopindex * X will delay the next action by X seconds, the second time through the loop is will delay the next action by 2*X seconds,etc . But the loop will run through all its repeats straight after each other and then move onto the next condition the waits sort of buffer the next actions ... the wait does not pause the whole program only the actions following it into a delayed list of actions to complete as their time comes up. Make sense ?

    last question - you could still use a global variable (gvDelay ?) and set this (in code) to random(x,y) before setting the loop to start. Don't think upon defining a variable you can set it to a random number.

    edit - v4 ... .capx?dl=0

  • Rampack

    So I tried repeating this function for a 2nd object, and I cannot get it to work. I recreated the global values with the new name for the new object, and tried both just recreating the events and actions, as well as just copy pasting, and changing the gv's and sprites, and the 2nd object will not spawn. Any idea what I may be doing wrong? I also tried using a different spawn sprite just to see if that was it for some reason, which it wasnt.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • how about ?

  • Ram, you have me very confused now lol. So, I see where your gameplay spawns 2 different types of units, but I don't see separate code for the 2nd unit, or even the sprite for it anywhere.

    After looking more, I see how it has something to do with the alientype command you have built in, but im pretty lost on how you are using that function.

    In my case here, I have currently (and may have more in the future as I expand) 3 different sprites I want to be able to spawn from these points, and like the one you already helped me with, I want to be able to set their own spawn number, and spawn frequency.

  • The 4 different types of aliens are just the frames in the sprites animation . Every time you click the "next wave button" (spButtonRestart) in event 4 it adds 1 to the gvAlienType and this is used to set animation frame after the alien sprite (spAlien) is spawned from a random spSpawnPoint in event 2.

    the sprite for spAlien consists of 4 frames each with a different alien graphic - animation speed set to 0.

    The spawn number and frequency are also set in event 4 ( global variables - gvNumberOfAliens and gvDelayPerAlienSpawn)

  • Ok I get it. How about actually setting in this case, each animation frame to have different spawn numbers, and different spawn frequency.

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