Typically there is a single instance of StateManager that ends up being stored at game.state, and a “default” game State instance at game.state.states.default. In this post I will be mainly writing about the StateManager, but I will also be touching base on State Objects as well.
For starters lets take a look at a stupid simple phaser project that is just a single create method, attached to a single State Object that will be the default state for the StateManager.
When I make a new instance of a phaser game, the object that I pass to the main Phaser.game constructor, after the id of the container element, will become the default game state object.
There are many methods for an instance of StateManager one of the most important of them, may be StateManger.add, as it allows me to write more than one State object for a project. With the add method the above demo can also me written like this.
This approach may be more desirable as it allows me to break things down into many separate files, one for each State Object. Instead of having everything in my main.js file, I could have a default.js (or maybe boot.js), load.js, title.js, game.js, ect.
In the above example I am just giving a plain old Object as the State, but it can also be An Instance of a Phaser State Object, or even a function.
Still I do not mainly make my states that way, I just give them a key that descibes the state, and will be used in other states to start it when needed, object that at a minimum has at least a create method, and maybe use the AutoState bool as the last argument in place of a game.state.start(key).
The start method is another must know, for both starting a state, as well as for changing between them. This allows for flow between many different States in one of my projects.
When starting a state at a minamum I must give the key of there state to start
But I can give up to four arguments that have to do with clearing caches, and also the option to pass a param object to an init method of the state if it has one. This is very useful when it comes to any kind of state in which there are different ways in which the state can be initialized, such as a game state in which there is a player progress object that can be passed to it to set up the game where the play left off in the game.
Thats it with the state manager for now, so far I do not have a lot of posts on phaser, but
rest assure this post will be updated in the future as my content on phaser grows. In time there Will be a collection of posts that I will be linking together.
If you are new to phaser start by playing around with state machines, you might also want to check out my getting started post on phaser.