liorfrenkel Posted February 8, 2016 Share Posted February 8, 2016 Hi, I have this code: console.log("top dead: " + this.topGrp.countDead()); // 3 this.topGrp.forEachDead((child) => { console.log("destroy top"); // only 2 times in chrome's console child.destroy(); }, this); topGrp is a group the function countDead() returns 3, and then forEachDead is iterating only 2 time, I see this in the console and only 2 object in my game are destroyed... help?? thanks! Lior Link to comment Share on other sites More sharing options...
Zeterain Posted February 8, 2016 Share Posted February 8, 2016 I suspect it has to do with the fact that you are destroying the children in the loop. I haven't tested anything, but I'm reading through the code for Group.iterate (https://github.com/photonstorm/phaser/blob/v2.4.4/src/core/Group.js#L1702) and I see that they using a for loop that checks the length of Group.children after each iteration. Because you are destroying children in the loop, the length of Group.children changes, causing the iteration to quit prematurely. liorfrenkel 1 Link to comment Share on other sites More sharing options...
liorfrenkel Posted February 8, 2016 Author Share Posted February 8, 2016 yes I think you are right @Icculus I tried adding the dead to a temp array and then destroying the children afterwards and everything is working. Thanks a lot! Zeterain 1 Link to comment Share on other sites More sharing options...
fillmoreb Posted February 8, 2016 Share Posted February 8, 2016 You could also try iterating through the children in reverse and checking for alive/dead: for(var i = this.topGrp.children.length-1; i >= 0; i--){ if(!this.topGrp.children.alive){ console.log("destroyed"); this.topGrp.children.destroy(); } } It'll run quicker and use less memory than building a second array. liorfrenkel and Zeterain 2 Link to comment Share on other sites More sharing options...
Recommended Posts