Move Routes
ThemoveRoutes method allows you to give a character (player or event) a sequence of movement instructions to follow. This is useful for creating patrol paths, scripted movements, or complex movement patterns.
Basic Usage
ThemoveRoutes method accepts an array of movement instructions and returns a Promise that resolves when all routes are completed.
Movement Instructions
Direction Enums
You can use theDirection enum directly:
Move Helper Functions
TheMove object provides convenient helper functions for common movements:
Tile-Based Movements
Tile movements calculate the distance based on tile size and player speed:Turn Commands
Turn commands change the character’s direction without moving:Wait/Delay
You can add delays between movements:Callback Functions
You can use callback functions to generate dynamic routes:Nested Arrays
Routes can be nested and will be automatically flattened:Advanced Features
Stuck Detection
When a character gets blocked by an obstacle or collision, you can handle it using theonStuck callback:
Stuck Detection Options
-
onStuck: Callback function called when the player is blocked- Parameters:
player: The player instance that is stucktarget: The target position the player was trying to reachcurrentPosition: The current position of the player
- Return value:
falseorundefined: Cancel the routetrue: Continue trying to reach the target
- Parameters:
-
stuckTimeout: Time in milliseconds to wait before considering the player stuck (default: 500ms)- The player must be unable to make progress for this duration before
onStuckis called
- The player must be unable to make progress for this duration before
-
stuckThreshold: Minimum distance change in pixels to consider movement progress (default: 1 pixel)- If the player moves less than this distance over the
stuckTimeoutperiod, they are considered stuck
- If the player moves less than this distance over the
Example: Handling Obstacles
Example: Continue After Obstacle Removal
Combining Movement Types
You can combine different movement types in a single route:Player Movement
For player-specific movements, you can use callbacks to check player state:Event Movement
Events can also use moveRoutes for patrol patterns:Infinite Routes
For characters that need to repeat a movement pattern indefinitely:Route Completion
ThemoveRoutes method returns a Promise that resolves when all routes are completed: