For the past few days I have been experimenting with Groups in Phaser ce. Today I worked out some examples that have to do with nesting groups. In other words a group is also a kind of display object, just like that of a Sprite, so another group can be added to a group, along with other display objects. So in this post I will be covering some examples of nesting groups inside of groups, from simple hello world examples, to some that are starting to resemble a game.
The basic idea here is that I just add another group to a group with the Group.add method.
In a nut shell thats all there is to it.
For a more complex use case example of nested groups I made a module that might evolve into a solution for making a game that involves the release of waves of enemies over time. That is a game where I would want a collection of waves, and each wave would hold a collection of enemies. In this kind of game I might always want another group to hold a cache of enemies, that will be released at a certain rate into another group where the enemies will actually be updated on each frame tick.
So to help with the complexities of all this I will want to make some kind of module that will house all nested groups, as well as a bunch of methods that help work with these groups. So In this sub section I will be writing about the round module, that will take care of all of this for me.
So I start off by making an object literal that will house all the properties, and methods. The first methods that I make for these kinds of projects is a setup methods of some kind. Here I offset some logic that would otherwise go in the create methods of the phaser state object that uses this module.
When the setup method is called in the create methods groups will be created for waves, cache, and active. They will start out empty, but can be populated later in the create method, or elsewhere with another method round.genWaves.
This is the method that will add a new nested group to the waves group. This nested group will then hold a collection of enemies, and I can also set up any additional stuff that I want to do for each enemy here as well, such as enabling input.
There is then a need for methods that can be used to add all the enemies in a wave into a cache group, from there they will then be released into another group where the enemies will actually then be active in the game. For this there is the nextWave, and release methods.
Finally I will want to add in a method that will be called from the update method of the game state that will be called for each frame tick. Here I am just defining the logic that is to apply to each enemy that is in the active group.
For this simple example the enemies are just moving from the top of the screen down to the bottom of the screen. During this time the player can click on them to destroy theme, else if they reach the bottom the player looses hit points. Not much I know, but that is the basic mechanics of many defense strategy games.
So now that I have my module I can use it in a phaser state object, and get this to start to resemble a game of sorts.