Today in this post on Phaser ce I will be writing about removing Sprites, and other display objects from a group. There is of course the Group.remove method, but by default it will not destroy the sprite. In some cases this is desirable if I want to move the sprite from one Group to another, or just remove it from a group in place it directly in the world. However it other projects, where Sprites are something that are created and destroyed on demand, then it is important to know about the few little pitfalls when it comes to removing sprites from a group in phaser.
The content of this post is on removing display objects from a group in phaser ce. I have another post on groups in general, and another post on getting started with phaser that may be of interest if you are new to using groups in phaser, or phaser in general.
So lets start out by covering some quick basics about removing Sprites, or display objects in general from a group in phaser cd, by looking at some simple examples. This will involve creating two groups, removing display objects from one, and adding to another. It will also cover how to truly remove display objects from the cache completely using Group.remove.
For these examples I started out my creating two groups called group1, and group2. I then add a bunch of text objects to group one using Group.add.
After doing so the child length of each group is as expected.
So the Group.remove Method works by passing a reference to the child that I want to remove as the first argument. In order to do that I need a reference first, one way to do so is to just grab one from the children array in the group as in this example. The second argument that I pass to Group.remove is a boolean that if true will truly destroy the display object, rather than just removing it from the group. By default this value is false, but I just pass a false value anyway for the sake of example.
The final boolean has to do with making it so an event does not fire, more on that later in the events section of this post.
So if I want to truly, and fully remove a display object from a group, I can just call the destroy method of the child in the group. I can also set the second argument of Group.remove to true.
by looking at the length of the group, or what is going on in the phaser cache, I can confirm first hand that these methods are working as advertised in the docs. Just remember that there is a difference between removing a display object from a group, and removing a display Object from a phaser game instance completely.
There are a few events that can be attached to display objects, the two of interest with respect to the content of this post are onAddedToGroup, and onRemoveFromGroup.
The third argument that can be given to Group.remove is a boolean that makes the onRemovedFromGroup event not fire it’s callback method.
Dow for an interesting example that might start to resemble the beginnings of an actual game. In this example I have two groups. One is a cache of Sprites that is positioned outside the bounds of the game world, and the other is positioned inside the game world. After a certain amount of time a Sprite from the cache Group is moved from the cache to the active group inside the game world. I made these sprites input enabled so when you click on them they are then moved back to the cache group.