pureMVC simple StateMachine utility

*Update see this post for the latest version*

Here is an implementation of a simple State Machine for pureMVC that I whipped up for a project. This is really useful for anyone creating applications, honest, its saved my life a few times :) It suddenly occurred to me that because its so simple I’ve been taking its power for granted, and I should share it for the greater geek good. I’ve been using it in combination with trilec’s command chaining utility as a way of controling my command structures.

Have put the code in the svn on my google code page and will prepare a download when I’ve documented it.

Initiate the StateMachine

// instanitate 
var stateMachine:StateMachine = new StateMachine();
// register with the facade before registering any commands
// create a state 
var state:State = new State(ApplicationStates.STATE_ONE)
// register the commands to be triggered when 
// entering and exiting that state 
// repeat for each state (this can be done any time)
state = new State(ApplicationStates.STATE_TWO)

Changing State
This is very simple, just send a notification from anywhere within the pureMVC framework with a new state as its body:

// create a state 
var state:State = new State(ApplicationStates.STATE_TWO)

[kml_flashembed movie=”http://www.revisual.co.uk/uploads/2008/oct/statemachine/state_machine_demo.swf” height=”300″ width=”470″ /]

5 thoughts on “pureMVC simple StateMachine utility

  1. neil, was thinking about implementing a state machine for a current flex pmvc proj im working on so this could be interesting. but looking at it, i’m wondering, what differentiates your state machine from pmvc’s macrocommands? that’s also a way of chaining your command structures, right?

  2. hi rich,
    my state machine is simply away of centralising and managing the commands (Simple or Macro). This has been my main problem with mvc.

    so you would create macro commands for each state and phase (enter, exit), and register them with the State Machine. Then all you need to do is tell the State Machine to change state, and it will do all the work.

    so to answer your question, its not an alternative for the pureMVC commands, but away of managing and implementing them.

    I’d also recommend using trelic’s command chaining util (there’s actually a bug in it, so I can send you my fix, if you’ld like) its a good way of chaining commands (synch and asynch) together into processes, while keeping all the info in one place.

  3. Hi Neil,

    I am new to pmvc and have recently installed the AS3 multicore version, compiled the “helloflash”, “sequential” and “minimalist” demos and am now moving on to some others like the “changing state” example above.

    Do you have any simple example sources available that can be compiled using mxmlc and your unified loader only? I’m not a flex programmer (sorry).

    Many thanks and have a great festive seaon.

  4. HI, am also doing R&D on State Management in pureMVC to achieve in my project, How you are maintaining state if it is ASYNC or SYNC based actions performed in the UI? and how to configure FSMInjector?

Leave a Reply

Your email address will not be published. Required fields are marked *