tags: animation, state_machine, input title: Animation State Machine brief: This example demonstrates how to create a character animation system using a Finite State Machine (FSM) with smooth transitions between different character states. author: The Defold Foundation scripts: knight.script
This example shows how to create a responsive character animation system using a Finite State Machine (FSM). The character can smoothly transition between different states like idle, running, jumping, attacking, and crouching based on player input. This is a fundamental technique used in most 2D platformers and action games.
State Machine: A design pattern where an object can be in only one state at a time, with clear rules for transitioning between states.
Input Priority: A system that determines which actions take precedence when multiple keys are pressed simultaneously.
Animation Transitions: Smooth changes between different animations, often with intermediate "transition" animations.## Key Concepts
State Machine: A design pattern where an object can be in only one state at a time, with clear rules for transitioning between states.
Input Priority: A system that determines which actions take precedence when multiple keys are pressed simultaneously.
Animation Transitions: Smooth changes between different animations, often with intermediate "transition" animations.
The example consists of two main game objects:
knight : The animated character. Contains:
knight.script) that implements the state machine logic, handles input, and manages animation transitions.gui : The user interface. Contains:
control.gui) that has 6 nodes displaying states and text description for the example.control.gui_script) that receives messages from the knight and updates the visual state indicators.Note:
The GUI in this example is not required for understanding the state machine logic, it only visually shows the active animation state. You can view the GUI source in the project files on Github still though.
The sprite component uses a flipbook animation that is set up in an atlas:
For this example we used the Free Knight Character by Nauris 'aamatniekss' available here: https://aamatniekss.itch.io/fantasy-knight-free-pixelart-animated-character
The atlas contains multiple animations for different character states:
| Key | Action |
|---|---|
| Left Arrow / Right Arrow | Move left/right |
| Space | Jump |
| X | Attack |
| C | Crouch (hold to stay crouched) |
The character uses a finite state machine - a programming pattern where the character can only be in one "state" at a time. Each state can define certain things like:
The system processes input with priorities: Attack > Jump > Movement > Crouch/Stand > Turning. This ensures that important actions (like attacking) can interrupt less important ones (like walking).
State Machine: A design pattern where an object can be in only one state at a time, with clear rules for transitioning between states.
Input Priority: A system that determines which actions take precedence when multiple keys are pressed simultaneously.
Animation Transitions: Smooth changes between different animations, often with intermediate "transition" animations.