phaserjs/phaser/loading-assets

Claude Code and agent skill by phaserjs/phaser

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Load files with the Phaser 4 Loader (this.load): images, spritesheets, atlases (JSON, XML, multiatlas, Aseprite, compact atlasPCT), compressed textures, audio, video, JSON, tilemaps, fonts and packs. Covers preload vs this.load.start(), setBaseURL/setPath/setPrefix and the loader config, progress events, retries, aborting with an AbortSignal (4.3), CORS and the caches. Use when making a loading screen or progress bar, a preloader Scene, loading levels on demand, or when a file fails to load or a texture from another domain shows nothing. Corrects old habits: maxRetries is 5 with backoff, a duplicate key is skipped silently, listeners use Phaser.Loader.Events constants. Triggers on: preload, this.load, spritesheet, atlas, asset pack, loading screen, progress bar, loaderror, CORS, AbortController, this.cache.

Install

npx vskill@latest install phaserjs/phaser/loading-assets

SKILL.md

The same SKILL.md was published earlier as phaserjs/phaser/loading-assets.

Loading Assets

The Loader (this.load) queues files, downloads them in parallel, and puts them into global caches that every Scene shares.

Source: src/loader/LoaderPlugin.js, src/loader/File.js, src/loader/filetypes/, src/loader/events/, src/cache/CacheManager.js. Related: scenes, audio-and-sound, animations, text-and-bitmaptext, game-setup-and-config.

Quick start

js
class Game extends Phaser.Scene
{
preload ()
{
this.load.setPath('assets/');
this.load.image('sky', 'sky.png');
this.load.spritesheet('dude', 'dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.atlas('ui', 'ui.png', 'ui.json');
this.load.audio('jump', [ 'jump.ogg', 'jump.mp3' ]);
}
create ()
{
// Everything queued in preload is ready here
this.add.image(this.scale.width / 2, this.scale.height / 2, 'sky');
}
}

The Loader starts by itself after preload(). Anywhere else, you must start it (see below).

File types

Every method also takes a config object ({ key, url, ... }) or an array of them. A relative URL gets baseURL + path in front.

MethodNotes
image(key, url)url can be [ texture, normalMap ] for lighting
spritesheet(key, url, { frameWidth, frameHeight, startFrame, endFrame, margin, spacing })Fixed-size frames, by index
atlas(key, textureURL, atlasURL)JSON Hash or Array (TexturePacker). atlasXML, unityAtlas for other formats
multiatlas(key, atlasURL, path, baseURL)One JSON, many images
atlasPCT(key, atlasURL, path, baseURL)Phaser Compact Texture atlas (.pct), one or more pages
aseprite(key, textureURL, atlasURL)Then this.anims.createFromAseprite(key), see animations
texture(key, { ASTC, ETC, S3TC, ..., IMG })Compressed textures. WebGL only
svg(key, url, { width, height, scale })Rasterised to a texture
audio(key, urls, config) / audioSprite(key, jsonURL, audioURLs)See audio-and-sound
video(key, urls, noAudio)Only picks a playable URL: the Video Game Object streams it later
json(key, url, dataKey), xml, text, binary, html, cssdataKey keeps one property, such as 'level1.enemies'
tilemapTiledJSON(key, url), tilemapCSV, tilemapImpacttilemapImpact is JSON too
bitmapFont(key, textureURL, fontDataURL)XML font data only
font(key, url, format, descriptors)Web font through the FontFace API. format defaults to 'truetype'
glsl(key, url)Shader source into this.cache.shader, see custom-rendering
animation(key, url)JSON passed to this.anims.fromJSON
pack(key, url), script, scripts, plugin, scenePlugin, sceneFile, htmlTexture

Use spritesheet for a grid of same-sized frames, and atlas for packed frames with names.

URLs, keys and the loader config

js
this.load.setBaseURL('https://cdn.example.com/'); // before the path
this.load.setPath('levels/1/'); // before each relative URL
this.load.setPrefix('L1.'); // before each key, not the URL
// Loads https://cdn.example.com/levels/1/boss.png, cached as 'L1.boss'
this.load.image('boss', 'boss.png');
  • Each setting applies to the files added after it, so set it first. Since 4.3 this includes the base URL: a file keeps the base URL the Loader had when the file was added.
  • Absolute URLs (http://, https://, //, data:, blob:) skip the path and base URL.
  • setPath and setBaseURL add the trailing /. If you set this.load.path directly, add it yourself.
  • With no URL, the key and the default extension make one: this.load.image('logo') loads logo.png.
  • The Loader is reset each time its Scene starts, back to the Scene's or game's loader config.

The defaults for every Scene go in the game config:

js
new Phaser.Game({
width: 1024,
height: 768,
loader: {
baseURL: 'https://cdn.example.com/',
path: 'assets/',
prefix: '', // 4.3: the game config key prefix
crossOrigin: 'anonymous',
maxParallelDownloads: 32, // 6 on Android
maxRetries: 5,
imageLoadType: 'XHR' // or 'HTMLImageElement'
}
});

A Scene can override these with a loader object in its own Scene config. src/core/typedefs/LoaderConfig.js lists every key.

Progress and events

Events are emitted by this.load. Its listeners are removed when the Scene shuts down, so you don't need to remove them.

js
preload ()
{
const { width, height } = this.scale;
const bar = this.add.graphics();
this.load.on(Phaser.Loader.Events.PROGRESS, (value) =>
{
bar.clear();
bar.fillStyle(0xffffff);
bar.fillRect(width / 4, height / 2 - 15, (width / 2) * value, 30);
});
this.load.on(Phaser.Loader.Events.FILE_LOAD_ERROR, (file) =>
{
console.warn('Failed:', file.key, file.src);
});
this.load.once(Phaser.Loader.Events.COMPLETE, () => bar.destroy());
this.load.image('sky', 'assets/sky.png');
}
ConstantStringArguments
START'start'(loader)
ADD'addfile'(key, type, loader, file)
PROGRESS'progress'(value), 0 to 1. It can go down if files are added while loading
FILE_PROGRESS'fileprogress'(file, percent), XHR files only
FILE_LOAD'load'(file), downloaded but not yet processed
FILE_LOAD_RETRY'loadretry'(file, event, retries) (4.3)
FILE_LOAD_ERROR'loaderror'(file, event), after the last retry
FILE_COMPLETE'filecomplete'(key, type, data), processed and cached
FILE_KEY_COMPLETE'filecomplete-'Add type + '-' + key, such as 'filecomplete-image-sky'
POST_PROCESS'postprocess'(loader)
COMPLETE'complete'(loader, totalComplete, totalFailed)

The Scene's own update() doesn't run until create() has.

Loading outside preload

js
loadLevel (n)
{
this.load.json(`level${n}`, `assets/level${n}.json`);
this.load.once(Phaser.Loader.Events.COMPLETE, () => this.buildLevel(n));
this.load.start();
}
  • Without start() nothing loads. If the Loader is already running, start() does nothing, and the new files join the current load.
  • To add files that depend on another, listen for its key event during the load: this.load.once(Phaser.Loader.Events.FILE_KEY_COMPLETE + 'json-level1', (key, type, data) => this.load.image(data.images)).

Packs

A pack is a JSON file listing files. Each section can set its own baseURL, path, prefix and defaultType, and each entry is the config object of a load method, with a type:

json
{
"level1": {
"path": "assets/level1/",
"files": [
{ "type": "image", "key": "bg", "url": "bg.png" },
{ "type": "atlas", "key": "tiles", "textureURL": "tiles.png", "atlasURL": "tiles.json" },
{ "type": "spritesheet", "key": "coin", "url": "coin.png", "frameConfig": { "frameWidth": 16, "frameHeight": 16 } }
]
}
}
  • this.load.pack('level1', 'assets/pack.json') loads every section. Pass a section name as the third argument (dataKey) to load only that one. The second argument can also be the pack object itself.
  • A section's baseURL works since 4.3. Before, it was ignored.
  • this.load.removePack('level1') removes the textures, animations and cache entries a loaded pack added.
  • A pack in the Scene config (super({ key: 'Boot', pack: { files: [ ... ] } })) loads before preload() runs, so a progress bar can use its images.

Retries, timeouts and aborting

  • A request that errors or times out is retried maxRetries times, 5 by default, waiting 100 ms and doubling up to 5 s between tries (4.3). So a network or CORS failure takes about 3 s to emit loaderror. Set loader: { maxRetries: 0 } to fail at once.
  • An HTTP error status, such as 404, fails at once without retries.
  • For one file, pass xhrSettings: { maxRetries: 1, timeout: 5000 } in its config object. this.load.maxRetries applies to files added after you change it. file.retries counts the retries so far, and file.retryAttempts is how many are left.
  • The loader config's user and password apply to every XHR request (fixed in 4.3). Its timeout and withCredentials are currently ignored (an engine issue), so set those per file in xhrSettings.
  • A Loader can abort its requests with an AbortSignal (4.3). An aborted file fails at once, with no retries, and emits loaderror. A file which starts after the abort isn't requested.
js
const controller = new AbortController();
this.load.xhr.signal = controller.signal; // every file from now on
this.load.image({ key: 'big', url: 'assets/big.png', xhrSettings: { signal: controller.signal } }); // or one file
controller.abort();

The signal only affects files loaded with XHR. It does nothing to HTML5 Audio, video, fonts, or images when imageLoadType is 'HTMLImageElement'.

Assets from another domain (CORS)

  • The server must send CORS headers. Without them an XHR load fails, and is retried before loaderror.
  • Set this.load.setCORS('anonymous') (or loader.crossOrigin) before loading images with imageLoadType: 'HTMLImageElement', and videos. Without it, WebGL can't use them.
  • Since 4.3, a Video from another domain loaded without CORS stops, logs a warning, and emits VIDEO_ERROR (Phaser.GameObjects.Events.VIDEO_ERROR) rather than throwing.
  • loader.localScheme (default [ 'file://', 'capacitor://' ]) lists schemes where a status of 0 counts as success. It has nothing to do with CORS.

Compact and compressed textures

js
// PCT: a small text atlas format. Frames are used like any atlas frame
this.load.atlasPCT('level1', 'assets/level1.pct');
// Compressed textures: the first format the GPU supports is loaded. IMG is the fallback
if (this.renderer.type === Phaser.WEBGL)
{
this.load.texture('logo', {
ASTC: 'assets/logo-astc-4x4.pvr',
ETC: 'assets/logo-etc2.pvr',
S3TC: 'assets/logo-bc3.pvr',
IMG: 'assets/logo.png'
});
}
else
{
this.load.image('logo', 'assets/logo.png');
}
  • PCT decoded data is kept in this.cache.atlas. The format is in docs/Phaser Compact Texture Atlas Format Specification/.
  • Compressed texture URLs need their extension, PVR or KTX containers. Each format can also be { type: 'PVR', textureURL, atlasURL } or { multiAtlasURL, multiPath } for an atlas. Sizes, colour spaces and tools are in docs/WebGL Compressed Textures/.

Fonts

js
this.load.font('Nokia', 'assets/nokia.ttf', 'truetype');
this.load.bitmapFont('pixel', 'assets/pixel.png', 'assets/pixel.xml');
// In create: the font key is the fontFamily
this.add.text(10, 10, 'Score', { fontFamily: 'Nokia', fontSize: 32 });
this.add.bitmapText(10, 60, 'pixel', 'Score', 32);

A web font which fails only logs a warning: no loaderror. See text-and-bitmaptext.

Caches

Textures go into the Texture Manager (this.textures). Everything else goes into this.cache: json, text, xml, binary, html, audio, video, shader, tilemap, bitmapFont, atlas, physics, plus this.cache.addCustom(name).

js
const level = this.cache.json.get('level1');
this.cache.json.exists('level1');
this.cache.json.remove('level1');
this.textures.exists('sky');
this.textures.remove('sky');

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
39,633
Repository forks
7,130
7-day trend score
53

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 17 May 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Grok Build (Cursor)
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/phaserjs/phaser/loading-assets/badge)](https://verified-skill.com/skills/phaserjs/phaser/loading-assets)

Something wrong with this skill or its page? Tell us.