Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Destroying all elements of a group with a parent


hackenstein
 Share

Recommended Posts

When I switch states (or levels) I destroy all elements of my layers to clean up the stage/world. This becomes problematic if I have nested groups. When a new group is created and another group is set as a parent, then _container of the new group will be added as a child to the parent group.

    destroyGroup: function destroyGroup(group, name)    {        var name = name  || 'undefined';        var length = group.length;        while(group.length > 0)        {            obj = group.getAt(0);            // console.log("Destroying: " + obj.type + " " + obj.name);                        /* Doesn't work!            if(obj.type == Phaser.GROUP)            {                console.warn("Found group in group!");                this.destroyGroup(obj, "sub-"+name);            }            */                        if(obj.destroy)                obj.destroy();            else if(obj.kill)            {                console.log(obj.type + " has no destroy function and will be killed instead");                group.remove(obj);                obj.kill();            }            else            {                console.error("Couldn't correctly remove " + obj.type +" object in " + name + " group!");                group.removeBetween(-1,1);            }                        // Safety            if(length == group.length)            {                console.error("Couldn't remove object in " + name + " group!");                break;            }            else                length = group.length;        }                group.destroy();    }

What I wish to do is destroy the elements of the child group as well, but because I have only it's _container I don't know how to do that. The _container doesn't even have a type. I found an issue about group.destroy not destroying the elements. I don't know if I'm using destroy wrong, but it would be really convenient if calling group.destroy(); would be all I had to do to remove the group with all it's children and grandchildren etc.

Link to comment
Share on other sites

  • 1 year later...

This is not working in my game. With or without the shutdown or removeAll. The objects from the previous state are still on screen below everything new that I add.

this.state.start('NewState', true,false);  second parameter which is suppose to clearWorld just doesnt work. 

Code: 

https://github.com/omarojo/Phaser-Test6/blob/master/scripts/MainMenu.js

Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

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