Sign in to follow this  
liorfrenkel

forEachDead is being called to little times

Recommended Posts

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

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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.

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.