Jump to content

Spawning enemies at random period


Daria K
 Share

Recommended Posts

Third time from me today :D,

So I want to spawn these customers at random millisecs every loop... The problem is that when the first number is generated it uses it(this specific number) for every loop after that? Any ideas? 

this is how I'm trying to do it, but I guess there is a better solution... I can't create a variable, since the same thing is going to happen:

game.time.events.loop(Math.floor((Math.random() * 10)*1000), this.createCustomer, this);

Link to comment
Share on other sites

I would do something like this:

var time_til_spawn = Math.random()*3000 + 2000;  //Random time between 2 and 5 seconds.
var last_spawn_time = game.time.time;

update() {  // This is your state's update loop
  var current_time = game.time.time;
  if(current_time - last_spawn_time > time_til_spawn){
    time_til_spawn = Math.random()*3000 + 2000;
    last_spawn_time = current_time;
    spawnCustomer();
  }
}
Link to comment
Share on other sites

It's best practice to put stuff in the update loop where not needed. The code example would check if the current time is less than the last spawn time on every tick. This doesn't have a bad influence on performance for one check, but over and over.. it would, I'd create something more like:

var spawnAllowed = true;
var enemyGroup;

create() {
    enemyGroup = this.game.add.group(); // create group
}

createNewEnemy() {
    if (spawnAllowed) {
         enemyGroup.create(x, y, cacheKey, frame); // add sprite to group
         queueEnemy(game.rnd.integerInRange(2500, 5000); // call enemy queue for random between 2.5 and 5 seconds
    }
}

queueEnemy(time) {
    this.game.time.addOnce(time, createNewEnemy); // add a timer that gets called once, then auto disposes to create a new enemy after the time given
}

Using the time.addOnce will mean there is only one eventListner at a time waiting for a timer callback. 

There isn't really any difference in code, other than this gives you more control over what happens when creating a new enemy and queueing enemies.. also, it clears up the update loop which is better on performance and generally better practice in game development :)

Link to comment
Share on other sites

Just popping in to say: if you add an event on the game's timer, Phaser is doing the same if statement under the covers; it won't save you anything performance-wise: https://github.com/photonstorm/phaser/blob/master/src/time/Timer.js#L451

Stylistically, though, it *is* nice not to have a bunch of if statements in your update function. ( =

Link to comment
Share on other sites

  • 1 year later...
On 4/5/2016 at 3:21 PM, megmut said:

It's best practice to put stuff in the update loop where not needed. The code example would check if the current time is less than the last spawn time on every tick. This doesn't have a bad influence on performance for one check, but over and over.. it would, I'd create something more like:


var spawnAllowed = true;
var enemyGroup;

create() {
    enemyGroup = this.game.add.group(); // create group
}

createNewEnemy() {
    if (spawnAllowed) {
         enemyGroup.create(x, y, cacheKey, frame); // add sprite to group
         queueEnemy(game.rnd.integerInRange(2500, 5000); // call enemy queue for random between 2.5 and 5 seconds
    }
}

queueEnemy(time) {
    this.game.time.addOnce(time, createNewEnemy); // add a timer that gets called once, then auto disposes to create a new enemy after the time given
}

Using the time.addOnce will mean there is only one eventListner at a time waiting for a timer callback. 

There isn't really any difference in code, other than this gives you more control over what happens when creating a new enemy and queueing enemies.. also, it clears up the update loop which is better on performance and generally better practice in game development :)

Thank you for the code. I used it like this to make it work:

this.game.time.events.add(time, createNewEnemy, this);

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...