Back to Phaser

Loading Assets - Reference

skills/loading-assets/references/REFERENCE.md

4.1.09.5 KB
Original Source

Loading Assets - Reference

Extended reference tables for the loading-assets skill. See ../SKILL.md for usage patterns and examples.

All File Types - Full Parameter Table

Every method below is called on this.load within a Scene.

MethodRegistered NameParametersDescription
image(key, url, xhrSettings)imagekey: string/config, url: string/string[], xhrLoads an image. URL array [texture, normalMap] for normal maps. Default ext: .png
spritesheet(key, url, frameConfig, xhrSettings)spritesheetkey, url, frameConfig: { frameWidth, frameHeight, startFrame, endFrame, margin, spacing }, xhrLoads a fixed-frame sprite sheet image
atlas(key, textureURL, atlasURL, textureXhr, atlasXhr)atlaskey, textureURL, atlasURL, xhr, xhrJSON texture atlas (TexturePacker JSON Hash/Array)
atlasXML(key, textureURL, atlasURL, textureXhr, atlasXhr)atlasXMLkey, textureURL, atlasURL, xhr, xhrXML texture atlas (Starling/Sparrow format)
multiatlas(key, atlasURL, path, baseURL, atlasXhr)multiatlaskey, atlasURL, path, baseURL, xhrMulti-texture atlas (single JSON, multiple images)
unityAtlas(key, textureURL, atlasURL, textureXhr, atlasXhr)unityAtlaskey, textureURL, atlasURL, xhr, xhrUnity texture atlas format
aseprite(key, textureURL, atlasURL, textureXhr, atlasXhr)asepritekey, textureURL, atlasURL, xhr, xhrAseprite animation atlas
audio(key, urls, config, xhrSettings)audiokey, urls (string/string[]), config { instances }, xhrAudio file(s). Provide array for format fallback
audioSprite(key, jsonURL, audioURL, audioConfig, audioXhr, jsonXhr)audioSpritekey, jsonURL, audioURL, audioConfig, xhr, xhrAudio sprite: single audio file + JSON marker data
video(key, urls, noAudio)videokey, urls (string/string[]), noAudio: boolVideo file. Set noAudio: true to skip audio track
json(key, url, dataKey, xhrSettings)jsonkey, url, dataKey: string, xhrJSON data. dataKey extracts a sub-property
xml(key, url, xhrSettings)xmlkey, url, xhrXML document
text(key, url, xhrSettings)textkey, url, xhrPlain text file
binary(key, url, dataType, xhrSettings)binarykey, url, dataType: typed array constructor, xhrBinary/ArrayBuffer data
html(key, url, xhrSettings)htmlkey, url, xhrHTML content as string
htmlTexture(key, url, width, height, xhrSettings)htmlTexturekey, url, width, height, xhrHTML content rendered to a texture
css(key, url, xhrSettings)csskey, url, xhrCSS file (injected into DOM)
glsl(key, url, xhrSettings)glslkey, url, xhrGLSL shader source file
svg(key, url, svgConfig, xhrSettings)svgkey, url, svgConfig: { width, height, scale }, xhrSVG rasterized to a texture
bitmapFont(key, textureURL, fontDataURL, textureXhr, fontDataXhr)bitmapFontkey, textureURL, fontDataURL, xhr, xhrBitmap font (texture + XML/JSON font data)
font(key, url, format, descriptors, xhrSettings)fontkey, url, format (default 'truetype'), descriptors: object, xhrWeb font file (ttf/otf/woff/woff2) via FontFace API
tilemapTiledJSON(key, url, xhrSettings)tilemapTiledJSONkey, url, xhrTiled JSON tilemap
tilemapCSV(key, url, xhrSettings)tilemapCSVkey, url, xhrCSV tilemap
tilemapImpact(key, url, xhrSettings)tilemapImpactkey, url, xhrImpact.js tilemap
animation(key, url, dataKey, xhrSettings)animationkey, url, dataKey, xhrAnimation JSON data (auto-added to AnimationManager)
pack(key, url, dataKey, xhrSettings)packkey, url, dataKey, xhrPack file (JSON manifest of other files to load)
script(key, url, type, xhrSettings)scriptkey, url, type (DOM element type attr), xhrJavaScript file (injected as script tag)
scripts(key, url, xhrSettings)scriptskey, url (string[]), xhrMultiple scripts loaded in order
plugin(key, url, start, mapping, xhrSettings)pluginkey, url, start: bool, mapping: string, xhrPhaser plugin JS file
scenePlugin(key, url, systemKey, sceneKey, xhrSettings)scenePluginkey, url, systemKey, sceneKey, xhrPhaser scene plugin JS file
sceneFile(key, url, xhrSettings)sceneFilekey, url, xhrExternal Scene JS file
texture(key, url, xhrSettings)texturekey, url (compressed texture config), xhrCompressed texture with format fallbacks (since 3.60)

All methods accept either positional arguments or a single config object as the first argument. All methods also accept an array of config objects as the first argument to batch-load multiple files of the same type.

Cache API

Assets are stored in global game-level caches (not per-Scene). Textures go into the Texture Manager; other data goes into typed sub-caches.

Cache Sub-Types

Access via this.cache.<type> within any Scene:

Sub-CacheAccessStores
this.cache.jsonJSON datathis.load.json() results
this.cache.textText datathis.load.text() results
this.cache.xmlXML documentsthis.load.xml() results
this.cache.audioAudio datathis.load.audio() decoded data
this.cache.binaryBinary/ArrayBufferthis.load.binary() results
this.cache.shaderGLSL shadersthis.load.glsl() results
this.cache.htmlHTML stringsthis.load.html() results
this.cache.tilemapTilemap datathis.load.tilemapTiledJSON() etc.
this.texturesTexture ManagerAll image/atlas/spritesheet textures

BaseCache Methods

Each this.cache.<type> sub-cache exposes these methods:

MethodDescription
get(key)Returns the cached item for the given key
add(key, data)Manually adds an item to the cache
remove(key)Removes an item from the cache
exists(key) / has(key)Returns true if the key exists in the cache
getKeys()Returns an array of all keys in the cache
destroy()Clears the entire sub-cache

Cache Events

Each sub-cache has an events property that emits events:

js
// Listen for items being added to the JSON cache
this.cache.json.events.on('add', (cache, key, item) => {
    console.log('JSON added:', key);
});

// Listen for removals
this.cache.json.events.on('remove', (cache, key, item) => {
    console.log('JSON removed:', key);
});

Texture Manager Methods

The Texture Manager (this.textures) provides texture-specific operations:

MethodDescription
exists(key)Returns true if a texture with this key exists
get(key)Returns the Texture object
remove(key)Removes a texture and frees its WebGL resources
getTextureKeys()Returns array of all texture keys
on('addtexture', (key) => {})Fires when any texture is added
on('addtexture-KEY', (texture) => {})Fires when a specific texture key is added (cross-scene notification)
on('removetexture', (key) => {})Fires when any texture is removed

Source File Map

FilePurpose
src/loader/LoaderPlugin.jsMain Loader class, accessed as this.load. Manages queue, parallel downloads, events
src/loader/File.jsBase File class. All file types extend this
src/loader/filetypes/ImageFile.jsthis.load.image()
src/loader/filetypes/SpriteSheetFile.jsthis.load.spritesheet()
src/loader/filetypes/AtlasJSONFile.jsthis.load.atlas()
src/loader/filetypes/AtlasXMLFile.jsthis.load.atlasXML()
src/loader/filetypes/MultiAtlasFile.jsthis.load.multiatlas()
src/loader/filetypes/UnityAtlasFile.jsthis.load.unityAtlas()
src/loader/filetypes/AsepriteFile.jsthis.load.aseprite()
src/loader/filetypes/AudioFile.jsthis.load.audio()
src/loader/filetypes/AudioSpriteFile.jsthis.load.audioSprite()
src/loader/filetypes/VideoFile.jsthis.load.video()
src/loader/filetypes/JSONFile.jsthis.load.json()
src/loader/filetypes/XMLFile.jsthis.load.xml()
src/loader/filetypes/TextFile.jsthis.load.text()
src/loader/filetypes/BinaryFile.jsthis.load.binary()
src/loader/filetypes/HTMLFile.jsthis.load.html()
src/loader/filetypes/HTMLTextureFile.jsthis.load.htmlTexture()
src/loader/filetypes/CSSFile.jsthis.load.css()
src/loader/filetypes/GLSLFile.jsthis.load.glsl()
src/loader/filetypes/SVGFile.jsthis.load.svg()
src/loader/filetypes/BitmapFontFile.jsthis.load.bitmapFont()
src/loader/filetypes/FontFile.jsthis.load.font()
src/loader/filetypes/TilemapJSONFile.jsthis.load.tilemapTiledJSON()
src/loader/filetypes/TilemapCSVFile.jsthis.load.tilemapCSV()
src/loader/filetypes/TilemapImpactFile.jsthis.load.tilemapImpact()
src/loader/filetypes/AnimationJSONFile.jsthis.load.animation()
src/loader/filetypes/PackFile.jsthis.load.pack()
src/loader/filetypes/ScriptFile.jsthis.load.script()
src/loader/filetypes/MultiScriptFile.jsthis.load.scripts()
src/loader/filetypes/PluginFile.jsthis.load.plugin()
src/loader/filetypes/ScenePluginFile.jsthis.load.scenePlugin()
src/loader/filetypes/SceneFile.jsthis.load.sceneFile()
src/loader/filetypes/CompressedTextureFile.jsthis.load.texture()
src/loader/filetypes/HTML5AudioFile.jsInternal: HTML5 Audio element loading (used by AudioFile)
src/loader/events/All loader event constants