Whats cool about init methods in Phaser State objects.
Because of the relationship with initialization methods of State Objects and the start method of the StateManager in phaser, it occurred to me that this is something that needs it’s own post.
If new to Phaser
A quick demo that will help understand why init methods are cool
So I would like to put together a real example, but making a game is something that takes a while, for for the sake of this post I will just throw together a stupid demo that is just some circles moving around a center point.
When I call game.state.start with just the key of the state, the demo will go by everything I put in the defaults object I defined in the init method, but as you can see the method accepts an argument. This argument can be used to set some different settings other than the default.
The param argument of StateManager.start
What makes init methods cool is that I can bass a config object of sorts to a state every time I start the state.
This will change some of the default values and make the demo behave different.
A real example would be having a game state that accepts an object in its init method that can set up the game in a way that would otherwise me a new game. This is why init methods come in handy, mainly I use them with my main game states in this way.
There are of course the core methods of state objects in phaser, but in some states an init method is called for as a means to set things up in a certain way before anything else is done.