Daniel Belohlavek Posted June 18, 2014 Share Posted June 18, 2014 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 More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 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 More sharing options...
user123456789 Posted June 18, 2014 Share Posted June 18, 2014 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 More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 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; } } });} Daniel Belohlavek 1 Link to comment Share on other sites More sharing options...
Daniel Belohlavek Posted June 19, 2014 Author Share Posted June 19, 2014 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts