Search code examples
javascriptcanvasgame-enginephaser-framework

Phaser.js: How to get or remove all sprites in one scene


I used to handle a group of sprites by push them into an array or sprite group which is build-in class in the Phaser. But I am seeking another simple way could get or remove all sprites in an scene. Does anyone have any idea to resolve this? Thanks a lot!


Solution

  • The Scene has a property children (link to documentation)

    You can get all Sprites, with the command:

    // where this = the current scene
    let allSprites = this.children.list.filter(x => x instanceof Phaser.GameObjects.Sprite);
    

    And then remove/destroy them all, like this:

    allSprites.forEach(x => x.destroy());
    

    Just iterating through the list of sprites, and calling the destroy function on each object.