As a developer for a gaming company that has a lot of feedback from its users I understand that people can get very passionate about the features that they’re used to having. I’m afraid I was a victim of that passion about a week ago when I saw this notice from Richard Davey(creator of Phaser).<\/span><\/p>\n This is just a heads-up for those who track issues that support for\u00a0nested<\/em>\u00a0Containers will be removed from a forthcoming version of Phaser (not 3.12, but probably 3.13).<\/p>\n The ability to create and use Containers will remain, but you will no longer be able to add a Container as a child of another. Source<\/a><\/p><\/blockquote>\n I understand and support this decision, but it did leave me with a problem to solve.<\/p>\n Coming from a background of flash I was used to creating MovieClips and adding other objects inside those clips.\u00a0 I especially use this for user interface components. In Phaser\u00a02 I use groups quite a bit to create UI.<\/span><\/p>\n To understand the problem, we need to understand a bit about groups and containers. Lately, I’ve been seeing a lot of confusion about groups and containers. It is especially confusing for those of us that are coming from Phaser 2. Groups in Phaser 3 do not work the same as in Phaser 2.<\/p>\n A common question I’ve seen is ” why can I not add a group to a group?”. Let’s have a look at what happens when you add a child to a container or group.<\/p>\n Here is some simple code that adds 4 images to a scene. We then add 2 images to each of the group and the container.<\/p>\n Now if we log out the scene to the console this is the result<\/p>\n <\/a><\/p>\n As you can see even though we have added the images, icon3 and 4 to the group, they are still on the display list of the scene. The group isn’t anywhere to be seen on the list at all! In Phaser 3\u00a0groups are used to organize game objects. You may group objects together for physics collisions, or simply to have an easy way to iterate through the children.\u00a0 In Phaser 3\u00a0 Sprites, images, texts are always children of the scene.\u00a0 That is except for containers.<\/p>\n Containers have their own display list. If you add a game object to a container it goes on the container’s list. Here is the output of the container.<\/p>\nGroups and Containers in Phaser 3<\/h2>\n
How Groups Work<\/h2>\n
create() {\r\n this.icon1 = this.add.image(100, 100, \"icon1\");\r\n this.icon2 = this.add.image(200, 200, \"icon2\");\r\n this.icon3 = this.add.image(300, 300, \"icon3\");\r\n this.icon4 = this.add.image(400, 400, \"icon4\");\r\n \/\/\r\n \/\/\r\n \/\/\r\n this.con = this.add.container();\r\n this.group = this.add.group();\r\n \/\/\r\n \/\/\r\n this.con.add(this.icon1);\r\n this.con.add(this.icon2);\r\n \/\/\r\n \/\/\r\n this.group.add(this.icon3);\r\n this.group.add(this.icon4);\r\n }<\/pre>\n
How Containers Work<\/h2>\n