Skip to main content

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
See the Display Animations Guide for more details on animation spritesheets.

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 a spritesheetResolver 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

  1. When a spritesheet is requested (e.g., when displaying a sprite), the engine first checks the cache
  2. If the spritesheet is not found and a resolver is configured, the resolver is called with the spritesheet ID
  3. The resolved spritesheet is automatically cached for future use
  4. If the resolver returns undefined or null, the spritesheet is not found and will not be displayed

Resolver Function Signature

Parameters:
  • id: string - The spritesheet ID that was requested
Returns:
  • 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

  1. Cache Management: Resolved spritesheets are automatically cached. If you need to invalidate the cache, you can clear it manually:
  2. Error Handling: Always handle errors gracefully in async resolvers:
  3. Performance: Use resolvers for spritesheets that are not needed immediately. Pre-load critical spritesheets in the static spritesheets array.
  4. 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