Jump to content

Removal of objects like group - How does this work?


bapho
 Share

Recommended Posts

Hi,

I'd like to know how this works? For example, we do this:

Let's assume sprite is a sprite object. And spritegrp is a group of sprites;

spritegrp = this.game.add.group();

spritegrp.add(sprite);

 

Now let's say I want to destroy spritegrp and all its children, I'd do the following:

spritegrp.removeAll();

 

If I try to access spritegrp in the console, there's still something in it. But I can see that it's been destroyed on stage. So, what does it remove and doesn't remove? Does it...

1.) Destroy the object and its subobjects completely, including any bound events (like timers and oninputdown on sprites inside it)

2.) Destroy the objects and its subobjects completely but leaves any bound events (like timers and oninputdown on sprites inside it) hanging

3.) Destroys the objects and its subobjects partially?

Link to comment
Share on other sites

I'm pretty new to Phaser but I have some experience with sprites and groups, so here are my thoughts...

spritegrp.removeAll() will remove all the children from spritegrp. It will not destroy the group or the children—both will still exist. If you do spritegrp.removeAll(true), the sprites will be removed from the group AND destroyed. The group will still exist. (Reference: https://phaser.io/docs/2.6.2/Phaser.Group.html#removeAll)

When you destroy a sprite, all of its children are also destroyed by default. It also destroys the input, events, and animation handlers on the sprite. (Reference: https://phaser.io/docs/2.6.2/Phaser.Sprite.html#destroy)

So the correct answer should be #1... as long as you do .removeAll(true) on the group. A better option might be to do spritegrp.destroy(), which will remove and destroy all children and then destroy the group. (Reference: https://phaser.io/docs/2.6.2/Phaser.Group.html#destroy)

Hope this helps.

Link to comment
Share on other sites

14 minutes ago, bapho said:

Ah thanks for that! So either way of destroying groups, it shouldn't result to a memory leak no?

Theoretically, no. But things can easily get complicated when you're doing lots of different things with sprites :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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