hackenstein Posted November 19, 2013 Share Posted November 19, 2013 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 More sharing options...
Alvin Posted November 21, 2013 Share Posted November 21, 2013 Hi,You can dogame.world.removeAll()to remove everything displayed on the screen if it's what you are after. KevinnFtw 1 Link to comment Share on other sites More sharing options...
hackenstein Posted November 21, 2013 Author Share Posted November 21, 2013 Yeah, that does work. I just thought destroy would be better, because the references are set to null, so the garbage collector can clean everything up. Not sure if this really is the case though... Link to comment Share on other sites More sharing options...
rich Posted November 22, 2013 Share Posted November 22, 2013 I agree, destroy should iterate and nuke cleanly. Again please can you file an issue on github (if you didn't already). Link to comment Share on other sites More sharing options...
hackenstein Posted November 22, 2013 Author Share Posted November 22, 2013 I have clarified this in crazysam's issue linked above.I'm always a little hesitant to write an issue, because I'm seldom sure that this is not the expected behaviour and I'm doing something stupid Link to comment Share on other sites More sharing options...
omarojo Posted June 18, 2015 Share Posted June 18, 2015 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 More sharing options...
eddieone Posted July 25, 2016 Share Posted July 25, 2016 For anyone finding this in the future, How's the weather on Mars? And to destroy all sprites in a group use group.removeAll(true) charlie_says, Juls and Tommy_ 3 Link to comment Share on other sites More sharing options...
Recommended Posts