State Machine - manno-xx/haxe-openfl-examples GitHub Wiki

The state machine allows the behavior of one object be dependent on its state. It is done in such a way that the behaviors are programmed in separate classes for maximum flexibility and ease of maintenance. The state machine in this example is the most basic one as displayed in the UML State Diagram below.

It's of a light that can be switched on and off. Pressing the space bar when its state is 'on' switches it off and pressing the space bar when its state is 'off' switches is on. So the response to the space bar being pressed is different depending on its state.

In the diagram below the black dot indicates the entry point of the state machine ('points' to the initial state). The lines (notice they have direction) identify possible transitions between states and the text at the lines identify the triggers that make the transition happen.

A more elaborate explanation with code examples van be found at: Game Programming Patterns

State Machine