Jump to content

How can I add a delay inside the update loop?


Daniel Belohlavek
 Share

Recommended Posts

Let's say I have a variable "assets" that is an object and contains several sprites.

 

Inside the update loop, I check the position of each sprite by iterating through the object. If a condition is true (the sprite is outside the game bounds), I want to wait a random amount of milliseconds and then give that sprite a new position without the need of destroying it and creating a new one. I want to use the same amount of sprites and continue re-spawning them until another condition is met (out of lives or whatever).

 

I tried using setTimeout() but it does not work inside the loop. Furthermore, I'm having problems figuring out how to "freeze" one sprite while the rest keep moving (that means I cant pause the update loop, although I don't even know if that's possible).

 

Please keep in mind that this is a mobile game and performance is important.

 

Is it really worth the effort? Should I just kill them and spawn new ones?

 

Thanks for reading.

Link to comment
Share on other sites

There are examples of sprite pooling which is usually the way you'd do this, but if you need it to work as described above, maybe something like this?

function update() {  // loop through our 'assets' group  assets.forEach(function(sprite) {    // check to see if the sprite is fully outside of the camera    if (!sprite.inCamera) {      // stop the sprite moving (if using physics)      sprite.body.velocity.setTo(0, 0);      // create a timer which waits between 10 and 500ms then repositions the sprite randomly within the game world      game.time.events.add(game.rnd.integerInRange(10, 500), function() {        sprite.position.setTo(game.world.randomX, game.world.randomY);      }    }  });}
Link to comment
Share on other sites

 

There are examples of sprite pooling which is usually the way you'd do this, but if you need it to work as described above, maybe something like this?

function update() {  // loop through our 'assets' group  assets.forEach(function(sprite) {    // check to see if the sprite is fully outside of the camera    if (!sprite.inCamera) {      // stop the sprite moving (if using physics)      sprite.body.velocity.setTo(0, 0);      // create a timer which waits between 10 and 500ms then repositions the sprite randomly within the game world      game.time.events.add(game.rnd.integerInRange(10, 500), function() {        sprite.position.setTo(game.world.randomX, game.world.randomY);      }    }  });}

 

Hmm.. Doesn't this result to a situation where each frame calls constantly timers for same object? Probably some sort of flag is needed to determine if the sprite re-positioning is pending.. or some alternative approach  :)

Link to comment
Share on other sites

Yeah you're right - schoolboy error there on my behalf... 

function update() {  // loop through our 'assets' group  assets.forEach(function(sprite) {    // check to see if the sprite is fully outside of the camera and is not currently being repositioned    if (!sprite.inCamera && !sprite.pendingMove) {      // set our 'pendingMove' flag so the object isn't checked again      sprite.pendingMove = true;      // stop the sprite moving (if using physics)      sprite.body.velocity.setTo(0, 0);      // create a timer which waits between 10 and 500ms then repositions the sprite randomly within the game world      // and resets our 'pendingMove' flag so it can be checked again in the future      game.time.events.add(game.rnd.integerInRange(10, 500), function() {        sprite.position.setTo(game.world.randomX, game.world.randomY);        sprite.pendingMove = false;      }    }  });}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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