Jump to content

Nuke the game and return to blank screen


owen
 Share

Recommended Posts

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

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.

 

Thanks

Owen

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

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