Email Updates RSS Subscribe

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.


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


Finite State Machine

Posted on November 30th, 2009 by Chris

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.

	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;
	public class Example4Main extends MovieClip
	{//Document Class
		public var _stateContainer:MovieClip;
		private var _sm:StateMachine;
		private var _redView:StateView;
		private var _comboView:StateView;
		private var _redVC:StateViewController;
		private var _comboVC:StateViewController;
		public function Example4Main()
			_button.addEventListener(MouseEvent.CLICK, handleButtonClick, false, 0, true);
			_redView = new RedState();
			_comboView = new ComboState();
			_sm = new StateMachine(true, true);
			_redVC = new StateViewController([_redView], _stateContainer);
			_comboVC = new StateViewController([_comboView], _stateContainer);
		private function handleButtonClick(e:MouseEvent):void
			trace("---BUTTON CLICK---");
			if (_sm.currentState == _redVC)
			{//make blue
			}//make blue
			{//make red
			}//make red
	}//Document Class

Download source code, documentation and examples here.

This product is licensed under GPLv3.

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