Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Better way to check all children in a group against conditional


jltjltjlt
 Share

Recommended Posts

I'm looking to check the x value of all children of an emitter and kill it if x > 200. Here's my immediate non-phaser method solution (this is in the update loop)

for (var i = 0, j = windEmitter.children.length; i < j; i++) {   var s = windEmitter.children[i];   if (s.x > 200) {       s.kill();   }}

Seems like there's got to be a more efficient way to use a Phaser.Group method ( like checkAll() or callAll() ) but I can't figure out how to format them and still include my conditional.

 

Thoughts?

Link to comment
Share on other sites

OP, your original code isn't so terrible. If the exact x coordinate isn't terribly important to you, you could try setting the lifespan of the particles when you create them. When their lifespan elapses (in milliseconds), they will be killed automatically.

 

@chongdashu, your use of forEach() is actually more expensive than the OP's method. You're passing a new anonymous function to forEach() every time update() is called, which means extra objects for GC (garbage collection) to clean up. While I agree that your code is easier on the eyes, you'd want to define the function once outside of update(), then pass it to forEach() by name. At that rate, you don't gain a whole lot in terms of readability, though. :(

 

OP, not sure what your windEmitter is meant to do, but another option would be to create a new class that extends Phaser.Particle. If your wind particles are complicated enough to justify an entire class, then you could solve this problem with a new class like this:

function WindParticle(game, x, y, key, frame) {    // Initialize particle    Phaser.Particle.call(this, game, x, y, key, frame);}WindParticle.prototype = Object.create(Phaser.Particle.prototype);WindParticle.prototype.constructor = WindParticle;WindParticle.prototype.update = function () {    // Check if this wind particle should die.    if (this.x > 200) this.kill();    // Call up to the parent class' update() function.    Phaser.Particle.prototype.update.call(this);};

Then you could something like this with windEmitter to make all of its particles based on your new WindParticle class:

windEmitter.particleClass = WindParticle;

Buuuuuut... That all seems like it's probably overkill unless your wind particles need to do other complex things. Setting particle.lifespan might just be the way to go. ;)

Link to comment
Share on other sites

@chongdashu, your use of forEach() is actually more expensive than the OP's method. You're passing a new anonymous function to forEach() every time update() is called, which means extra objects for GC (garbage collection) to clean up. While I agree that your code is easier on the eyes, you'd want to define the function once outside of update(), then pass it to forEach() by name. At that rate, you don't gain a whole lot in terms of readability, though.

 

Excellent point! I totally skipped over the more efficient part, and mistakenly just focused on what I thought was OP's desire for something looking a bit more elegant. You are absolute right that the anonymous inner function is terrible for efficiency. Thanks for pointing it out!

Link to comment
Share on other sites

Thanks for your answers everyone. 

 

@drhayes, there doesn't seem to be any significant performance problem though I haven't checked my script on a slower computer yet. I was just hoping to learn a way to leverage the Phaser code to accomplish the same thing. Upon inspection though, the Phaser specific callAll() and checkAll() (and others) are just for loops themselves.

 

Phaser.Group.forEach as @chongdashu suggested, with a predefined function, will probably be my route.

 

@zatch thanks for the detailed response! My particles are very simple, but I'll definitely be using that technique on something in the near future, very handy.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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