Email Updates RSS Subscribe
Line

This blog is created and maintained by the technical team at Hook in an effort to preserve and share the insights and experience gained during the research and testing phases of our development process. Often, much of this information is lost or hidden once a project is completed. These articles aim to revisit, expand and/or review the concepts that seem worth exploring further. The site also serves as a platform for releasing tools developed internally to help streamline ad development.

Launch
Line

Hook is a digital production company that develops interactive content for industry leading agencies and their brands. For more information visit www.byhook.com.

Line

Finite State Machine

Line
Posted on November 30th, 2009 by Chris
Line

Often in an application it is necessary to be able to define and then switch the application state based on either user input or data updates. The Finite State Machine (FSM) is our version of a State Machine in Actionscript 3. The FSM uses StateControllers to manage lists of State Objects. A State Object is anything that implements IState. The FSM includes 2 such objects: State, which is the most basic example and StateView which is useful for classes that are also DisplayObjects.

Getting Started

The FSM includes 2 controllers. The most basic is StateController. A StateController is a management interface for a list of state sub objects. When a StateController is told to exit or enter, the controller then in return tells all of its sub objects to exit or enter. It then waits until all of the sub objects have finished their exits/enters and dispatches and event notifying of its completion. All commands directed at a State should go through a StateController. The second is StateViewController. This is the controller for each display state of type StateView. This must contain a StateView object that it controls.

Essentially you define each new state as a StateController or StateViewController and pass it an array of the IState items and use the FSM to change states. We have included some examples of how this process works.

The FSM can execute state changes either sequentially or with blending. If blendTransitions is set to true the next state will start entering before the current state is finished exiting, allowing for overlapping transition animations.

Here is an example of a Document Class that implements the FSM. More examples are included below.

package
{
	import app.BlueState;
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import com.jac.fsm.GroupController;
	import com.jac.fsm.StateMachine;
	import com.jac.fsm.StateViewController;
	import com.jac.fsm.StateController;
	import com.jac.fsm.StateView;
	import flash.events.MouseEvent;
 
	public class Example4Main extends MovieClip
	{//Document Class
 
		//ELEMENTS
		public var _stateContainer:MovieClip;
		//ELEMENTS END
		private var _sm:StateMachine;
		private var _redView:StateView;
		private var _comboView:StateView;
 
		private var _redVC:StateViewController;
		private var _comboVC:StateViewController;
 
		public function Example4Main()
		{//Example4Main
 
			_button.addEventListener(MouseEvent.CLICK, handleButtonClick, false, 0, true);
 
			_redView = new RedState();
			_comboView = new ComboState();
 
			trace("Creation");
			_sm = new StateMachine(true, true);
 
			_redVC = new StateViewController([_redView], _stateContainer);
			_comboVC = new StateViewController([_comboView], _stateContainer);
 
			_sm.changeState(_redVC);
 
		}//Example4Main
 
		private function handleButtonClick(e:MouseEvent):void
		{//handleButtonClick
			trace("---BUTTON CLICK---");
			if (_sm.currentState == _redVC)
			{//make blue
				_sm.changeState(_comboVC);
			}//make blue
			else
			{//make red
				_sm.changeState(_redVC);
			}//make red
		}//handleButtonClick
 
	}//Document Class
 
}

Download source code, documentation and examples here.

This product is licensed under GPLv3.

Line
4 Responses to “Finite State Machine”
  1. iPhone Apps says:

    Very helpful information. Very helpful, great share.

  2. Медик says:

    Всё изложено…

    Медикаменты и лекарства лечат всё же…

  3. bumbus says:

    I figured it out myself now, I should first think before asking…:)

  4. bumbus says:

    Hi there,
    looks great! Im just playing around with it. I just ask myself how to handle this events? Or where do i have to register them?

    Perhaps you can provide an example?
    Thanx for listening


Leave a Reply

*

Line
Line
Pony