Tilde Posted November 18, 2015 Share Posted November 18, 2015 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 More sharing options...
sapptime Posted November 19, 2015 Share Posted November 19, 2015 I would assume that it impacts the foreach loop if you're destroying group memberd during the loop. I haven't tested to be sure, but instead of doing the foreach, I think you'd want to do something like enemies.callAll('someDestroyCheckFunc')... Tilde 1 Link to comment Share on other sites More sharing options...
sapptime Posted November 19, 2015 Share Posted November 19, 2015 Alternatively, you could flag the enemies for destroying in the forEach, then on your next update, destroy all the ones you flagged. Tilde 1 Link to comment Share on other sites More sharing options...
drhayes Posted November 19, 2015 Share Posted November 19, 2015 Yeah, mark them for destruction then destroy them after the loop 'cuz I bet the "destroy" call is messing up a loop index. Tilde 1 Link to comment Share on other sites More sharing options...
Recommended Posts