jltjltjlt

Better way to check all children in a group against conditional

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?

Share this post


Link to post
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. ;)

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.