owen Posted September 8, 2014 Share Posted September 8, 2014 Hi Can I wipe out the entire game (all layers and objects) and return to blank screen using a single command? Or do I need to cycle through all of the objects and kill/destroy them? ThanksOwen Link to comment Share on other sites More sharing options...
eguneys Posted September 9, 2014 Share Posted September 9, 2014 game.destroy(); will destroy everything. owen 1 Link to comment Share on other sites More sharing options...
owen Posted September 9, 2014 Author Share Posted September 9, 2014 Thanks for this. Related question - can I destroy all of the sprites in a game using a single command? (Not the game itself, just the sprites/objects)? Something like game.mylayer.objects.destroy() ? Link to comment Share on other sites More sharing options...
lewster32 Posted September 9, 2014 Share Posted September 9, 2014 You can iterate through any group or array of objects and destroy them individually, yes - but remember Phaser uses a tree structure for its display list, not a flat array. The destroy method of groups will however by default destroy its children too; this is controlled bt the first parameter in destroy. Link to comment Share on other sites More sharing options...
owen Posted September 9, 2014 Author Share Posted September 9, 2014 You can iterate through any group or array of objects and destroy them individually, yes - but remember Phaser uses a tree structure for its display list, not a flat array. The destroy method of groups will however by default destroy its children too; this is controlled bt the first parameter in destroy. But what if I have dozens of groups, do I have to destroy all of them individually? Can I do it from the game level, something like this:// destroy all groups and the objects within themgame.groups.forEach ( function (g) { g.destroy(); }, this); Link to comment Share on other sites More sharing options...
lewster32 Posted September 9, 2014 Share Posted September 9, 2014 Nearly, game.world.forEach should do it, since world is just a type of group that sits at the root of the display tree. owen 1 Link to comment Share on other sites More sharing options...
owen Posted September 16, 2014 Author Share Posted September 16, 2014 Right, this works but only for tiles.The sprites meanwhile are left behind on screen. if (game.world) { game.world.forEach(function (g) { if (g) g.destroy(); },this); }I figure that for sprites one must use kill instead of destroy so I tried the following to remove the sprites: if (game.world) { game.world.forEach(function (g) { if (g) g.kill(); },this); }...but it throws an error: Uncaught Type Error: undefined is not a function Is there a special way of removing sprites as opposed to tiles? EDIT: I've worked around this by adding all my game sprites into a group called gameSprites and then using gameSprites.destroy() to clear it all down when needed. It works fine this way but I'm sure there are better ways to do it. ThanksOwen Link to comment Share on other sites More sharing options...
lewster32 Posted September 16, 2014 Share Posted September 16, 2014 Sprites should also be destroyed in the same way; kill just temporarily 'hides' the sprite. What I'd recommend you do is create an array and every time you create a sprite, .push it to the array. Then when you want to clean up, you can simply go through that array and delete every sprite. Link to comment Share on other sites More sharing options...
Recommended Posts