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
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.
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.
real examples
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.
Conclusion
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.
happy coding.