Jump to content

Is it just me, or does Group.forEach act like Group.forMost?


Tilde
 Share

Recommended Posts

I consistently have a problem all over my project where using Group.forEach and its variants only affects most of the children being called on, and calling it repeatedly will get it closer to all targets as it narrows them down. For instance:

this.deathParticleEmitter.forEachExists(function(item) {  //console.log(this.deathParticleEmitter.getIndex(item));  var explosion = this.game.add.image(item.x, item.y, 'smallExplosion');  explosion.anchor.setTo(0.5, 0.5);  explosion.rotation = item.rotation;  explosion.animations.add('natural');  explosion.animations.play('natural', 12, false, true);  item.destroy();}, this);

Essentially, when an enemy explodes, this will ensure that everything its deathParticleEmitter emitted also explodes at the same time. It's a pretty cool effect. But whenever it happens in game, it always leaves about a third of the existing particles untouched. Here's another one:

enemies.forEach(function(enemy) {  if (!enemy.killed && enemy.spawnID <= this.despawnEnemiesTo && !enemy.inCamera) {    enemy.exists = false;    enemy.destroy(); // This is mostly for testing purposes, not sure if I'll keep it yet.  }}, this);

This is called when the player passes a checkpoint. It should destroy every enemy before that checkpoint if they're not in the camera. Since I have magic developer powers, I can cheat and go back to where I was in the level...and most of the enemies are gone, but some are still there (again, probably about a third). If I hack it to trigger the checkpoint again, then once again, most of the still-existing enemies will be destroyed. So they're all eligible for destruction, it's just that each time this code is run, it only applies to most of the children of the group.

 

And it happens in other places, too. Is this just a series of coincidental errors, or is there something else at play here?

 

EDIT: Even though I've idly had this problem for months, a thought just now occurred to me: Does the fact that I'm destroying children of the group screw up the forEach's iteration? I can't believe I put off making this thread and this just now hits me...Maybe it's useful to Rich?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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