*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 facade.registerMediator(stateMachine); // create a state var state:State = new State(ApplicationStates.STATE_ONE) // register the commands to be triggered when // entering and exiting that state stateMachine.registerStateChangeCommands(EnterStateOne, ExitStateOne, state); // repeat for each state (this can be done any time) state = new State(ApplicationStates.STATE_TWO) stateMachine.registerStateChangeCommands(EnterStateTwo, ExitStateTwo, 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) sendNotification(StateNoteNames.CHANGE_APPLICATION_STATE, state))
[kml_flashembed movie=”http://www.revisual.co.uk/uploads/2008/oct/statemachine/state_machine_demo.swf” height=”300″ width=”470″ /]