What to know about the state manager in phaser
When I start making a javaScript project they almost always involve some kind of main game loop, and in more advanced projects an array of objects that contain methods for each game state. In Phaser there is the StateManager, and State objects that are of concern when it comes to getting this done with phaser, rather than my usual vanilla js solutions.
If new to Phaser
If you are new to phaser you might want to check out my post on getting started with phaser, I also assume that you have at least some knowlage of html, css, and javaScript as I do not have any posts on how to get up to speed with that at this time. This is my standard hexo tag for all phaser posts where I want to inform readers that this is not a post for beginers.
The state manager object
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.
A very simple demo
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.
StateManager.add(key, State, autoStart)
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).
StateManager.start(key, clearWorld, clearCache,param)
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.
|
|
Conclusion
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.