Jump to content

What do people use for state machines in Phaser?


Recommended Posts



What do people recommend for implementing FSMs in Phaser? 





Since FSMs make it a lot easier to manage the complexity of game states, I try to use them in all my games. This is not the same as what Phaser calls states. That looks like it's more for screen management. This article explains the state pattern that I'm referring to pretty clearly. 


So I'm wondering how do people recommend implementing FSMs within a Phaser state? What patterns or plugins they use? Or do they roll their own? If roll your own, any advice on implementation? 


Or maybe my understanding of Phaser states is mistaken and they can be used for FSMs and I just don't know how? 

Link to comment
Share on other sites


I used both of those two:




Jakesgordon's StateMachine was very useful in a pinball machine to manage the state of every moving part and also the overall state of the pinball machine. I never missed a hierarchical fsm but that's what machina is supporting now.

Machina seems to be the more engeneered fsm- that's why I forced me to try it in my last project: to manage the state of a popping champagne bottle. Yeah that's right, I could have done it with a bunch of state variables in this case but it was for the sake of fun :) Not to mention that it was a full success. So I have still the dilemma that I have to choose one of them for my next project.


There is really no need to brew your own fsm.




Link to comment
Share on other sites

I don't use several Phaser.States during gameplay, I only use a Phaser.State for each screen, and the gameplay just counts as one screen. So there's one State for the main menu, one for the level select screen and one for the actual game. See Phaser.State examples in the forum here or here.
During the gameplay State I just use a variable to distinguish the gameplay situations. Very simple maybe, but for me it does the job. So something like this:
var GAME_INTRO = 0; // short animation and show "Level 1"var GAME_PLAYING = 1;var GAME_GAMEOVER = 2;// at start of gamethis._mystate = GAME_PLAYING;

And for example, when handling the player input first check if it's not during intro or game over

// don't handle input in intro or gameoverif (this._mystate == GAME_PLAYING) {    // handle player input}

In short, I guess my games aren't complex enough (yet) to require a Finite-state Machine.  :huh:

Link to comment
Share on other sites

  • 3 weeks later...

Thanks for raising this topic.  I had not been formally knowledgeable about FSMs, though had used similar methods in some cases.  The article you linked to is an excellent explanation of the concept and its variations.  Seems to me the concept of FSM is portable to Javascript, and by extension, to Phaser apps.  Phaser states, as I currently understand and use them, are wired to assumptions built into Phaser about expected create-update-render domains within each state.  But I can see how one would implement FSMs in JavaScript with the switch-case statement central to the flow.  Richard Davey (author of Phaser) would be an excellent person to comment on this.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...