Spritesheets Guide
This guide explains how to work with spritesheets in RPG-JS, including static spritesheets and dynamic spritesheet resolution.Overview
Spritesheets are image files containing multiple frames or tiles that are used to display characters, objects, and animations in your game. RPG-JS supports both pre-loaded static spritesheets and dynamic spritesheet creation through a resolver system.Static Spritesheets
Basic Configuration
Add spritesheets to your client module configuration:Using Presets
RPG-JS provides several preset helpers to simplify spritesheet configuration:- LPCSpritesheetPreset: For Liberated Pixel Cup (LPC) style character spritesheets
- FacesetPreset: For character face expressions in dialog boxes
- AnimationSpritesheetPreset: For animation sequences
Dynamic Spritesheet Resolver
The spritesheet resolver allows you to create spritesheets on-the-fly when they are requested but not found in the cache. This is useful for:- Loading spritesheets from external APIs
- Generating spritesheets programmatically
- Creating spritesheets based on runtime data
- Lazy loading spritesheets to reduce initial load time
Configuration
Add aspritesheetResolver function to your client module:
Asynchronous Resolver
The resolver can also be asynchronous, useful for loading spritesheets from APIs or files:Programmatic Generation
You can also generate spritesheets programmatically:How It Works
- When a spritesheet is requested (e.g., when displaying a sprite), the engine first checks the cache
- If the spritesheet is not found and a resolver is configured, the resolver is called with the spritesheet ID
- The resolved spritesheet is automatically cached for future use
- If the resolver returns
undefinedornull, the spritesheet is not found and will not be displayed
Resolver Function Signature
id: string- The spritesheet ID that was requested
SpritesheetDefinition- A spritesheet configuration object (synchronous)Promise<SpritesheetDefinition>- A Promise that resolves to a spritesheet (asynchronous)undefined | null- Indicates the spritesheet cannot be created
Spritesheet Definition
A spritesheet definition is an object with the following properties:Programmatic API
You can also set a resolver programmatically using the engine:Best Practices
-
Cache Management: Resolved spritesheets are automatically cached. If you need to invalidate the cache, you can clear it manually:
-
Error Handling: Always handle errors gracefully in async resolvers:
-
Performance: Use resolvers for spritesheets that are not needed immediately. Pre-load critical spritesheets in the static
spritesheetsarray. -
ID Patterns: Use consistent ID patterns to make resolver logic easier:
Example: Loading from CDN
Example: Fallback Chain
Combining Static and Dynamic
You can use both static spritesheets and a resolver together. Static spritesheets are loaded first, and the resolver is only called for spritesheets not found in the static list:See Also
- Display Animations Guide - Learn about animation spritesheets
- Sprite Components Guide - Add visual components to sprites
- Dialog Box Guide - Using facesets in dialogs