Navigation
These archived docs are for Phaser 2.3.0 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Loader

new Loader(game)

The Loader handles loading all external content such as Images, Sounds, Texture Atlases and data files.

The loader uses a combination of tag loading (eg. Image elements) and XHR and provides progress and completion callbacks.

Parallel loading (see enableParallel) is supported and enabled by default. Load-before behavior of parallel resources is controlled by synchronization points as discussed in withSyncPoint.

Texture Atlases can be created with tools such as Texture Packer and Shoebox

Parameters
Name Type Description
game Phaser.Game

A reference to the currently running game.

Source code: loader/Loader.js (Line 22)

Public Properties

[static] PHYSICS_LIME_CORONA_JSON : number

Source code: loader/Loader.js (Line 268)

[static] PHYSICS_PHASER_JSON : number

Source code: loader/Loader.js (Line 274)

[static] TEXTURE_ATLAS_JSON_ARRAY : number

Source code: loader/Loader.js (Line 250)

[static] TEXTURE_ATLAS_JSON_HASH : number

Source code: loader/Loader.js (Line 256)

[static] TEXTURE_ATLAS_XML_STARLING : number

Source code: loader/Loader.js (Line 262)

_withSyncPointDepth

A counter: if more than zero, files will be automatically added as a synchronization point.

Properties:
Name Type Description
_withSyncPointDepth; integer
Source code: loader/Loader.js (Line 173)

baseURL : string

If you want to append a URL before the path of any asset you can set this here. Useful if allowing the asset base url to be configured outside of the game code. The string must end with a "/".

Source code: loader/Loader.js (Line 76)

crossOrigin : boolean | string

The crossOrigin value applied to loaded images. Very often this needs to be set to 'anonymous'.

Type
  • boolean | string
Default Value
  • false
Source code: loader/Loader.js (Line 67)

enableParallel : integer

If true (the default) then parallel downloading will be enabled.

To disable all parallel downloads this must be set to false prior to any resource being loaded.

Source code: loader/Loader.js (Line 157)

<internal> game : Phaser.Game

Local reference to game.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 29)

hasLoaded : boolean

True if all assets in the queue have finished loading.

Default Value
  • false
Source code: loader/Loader.js (Line 50)

isLoading : boolean

True if the Loader is in the process of loading the queue.

Default Value
  • false
Source code: loader/Loader.js (Line 43)

<internal> maxParallelDownloads : integer

The number of concurrent / parallel resources to try and fetch at once.

Many current browsers limit 6 requests per domain; this is slightly conservative.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 167)

onFileComplete : Phaser.Signal

This event is dispatched when a file has either loaded or failed to load.

Params: (progress, file key, success?, total loaded files, total files)

Source code: loader/Loader.js (Line 121)

onFileError : Phaser.Signal

This event is dispatched when a file (or pack) errors as a result of the load request.

For files it will be triggered before onFileComplete. For packs it will be triggered before onPackComplete.

Params: (file key, file)

Source code: loader/Loader.js (Line 132)

onFileStart : Phaser.Signal

This event is dispatched immediately before a file starts loading. It's possible the file may fail (eg. download error, invalid format) after this event is sent.

Params: (progress, file key, file url)

Source code: loader/Loader.js (Line 112)

onLoadComplete : Phaser.Signal

This event is dispatched when the final file in the load queue has either loaded or failed.

Source code: loader/Loader.js (Line 91)

onLoadStart : Phaser.Signal

This event is dispatched when the loading process starts: before the first file has been requested, but after all the initial packs have been loaded.

Source code: loader/Loader.js (Line 84)

onPackComplete : Phaser.Signal

This event is dispatched when an asset pack has either loaded or failed to load.

This is called when the asset pack manifest file has loaded and successfully added its contents to the loader queue.

Params: (pack key, success?, total packs loaded, total packs)

Source code: loader/Loader.js (Line 102)

<internal> preloadSprite : Object

You can optionally link a progress sprite with setPreloadSprite.

This property is an object containing: sprite, rect, direction, width and height

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 60)

progress

The rounded load progress percentage value (from 0 to 100). See Phaser.Loader#progressFloat.

Properties:
Type Description
integer
Source code: loader/Loader.js (Line 2282)

progressFloat

The non-rounded load progress value (from 0.0 to 100.0).

A general indicator of the progress. It is possible for the progress to decrease, after onLoadStart, if more files are dynamically added.

Properties:
Type Description
number
Source code: loader/Loader.js (Line 2264)

resetLocked : boolean

If true all calls to Loader.reset will be ignored. Useful if you need to create a load queue before swapping to a preloader state.

Default Value
  • false
Source code: loader/Loader.js (Line 36)

useXDomainRequest : boolean

This method is deprecated and should not be used. It may be removed in the future.

If true and if the browser supports XDomainRequest, it will be used in preference for XHR.

This is only relevant for IE 9 and should only be enabled for IE 9 clients when required by the server/CDN.

Deprecated:
  • This is only relevant for IE 9.
Source code: loader/Loader.js (Line 142)

Public Methods

<internal> addToFileList(type, key, url, properties, overwrite)

Internal function that adds a new entry to the file list. Do not call directly.

Parameters
Name Type Argument Default Description
type string

The type of resource to add to the list (image, audio, xml, etc).

key string

The unique Cache ID key of this resource.

url string

The URL the asset will be loaded from.

properties object <optional>
(none)

Any additional properties needed to load the file. These are added directly to the added file object and overwrite any defaults.

overwrite boolean <optional>
false

If true then this will overwrite a file asset of the same type/key. Otherwise it will will only add a new asset. If overwrite is true, and the asset is already being loaded (or has been loaded), then it is appended instead.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 455)

atlas(key, textureURL, atlasURL, atlasData, format) → {Phaser.Loader}

Add a new texture atlas ('textureatlas') to the loader.

Texture Atlases can be created with tools such as Texture Packer and Shoebox

Parameters
Name Type Argument Description
key string

Unique asset key of the texture atlas file.

textureURL string

The url of the texture atlas image file.

atlasURL string <optional>

The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.

atlasData object <optional>

A JSON or XML data object. You don't need this if the data is being loaded from a URL.

format number <optional>

A value describing the format of the data, the default is Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 1023)

atlasJSONArray(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}

Add a new texture atlas ('textureatlas') to the loader. This atlas uses the JSON Array data format.

Texture Atlases can be created with tools such as Texture Packer and Shoebox

Parameters
Name Type Argument Description
key string

Unique asset key of the texture atlas file.

textureURL string

The url of the texture atlas image file.

atlasURL string <optional>

The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.

atlasData object <optional>

A JSON or XML data object. You don't need this if the data is being loaded from a URL.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 969)

atlasJSONHash(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}

Add a new texture atlas ('textureatlas') to the loader. This atlas uses the JSON Hash data format.

Texture Atlases can be created with tools such as Texture Packer and Shoebox

Parameters
Name Type Argument Description
key string

Unique asset key of the texture atlas file.

textureURL string

The url of the texture atlas image file.

atlasURL string <optional>

The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.

atlasData object <optional>

A JSON or XML data object. You don't need this if the data is being loaded from a URL.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 988)

atlasXML(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}

Add a new texture atlas ('textureatlas') to the loader. This atlas uses the Starling XML data format.

Parameters
Name Type Argument Description
key string

Unique asset key of the texture atlas file.

textureURL string

The url of the texture atlas image file.

atlasURL string <optional>

The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.

atlasData object <optional>

A JSON or XML data object. You don't need this if the data is being loaded from a URL.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 1007)

audio(key, urls, autoDecode) → {Phaser.Loader}

Add a new 'audio' file to the loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the audio file.

urls string | Array.<string> | Array.<object>

Either a single string or an array of URIs or pairs of {uri: .., type: ..}. If an array is specified then the first URI (or URI + mime pair) that is device-compatible will be selected. For example: "jump.mp3", ['jump.mp3', 'jump.ogg', 'jump.m4a'], or [{uri: "data:<opus_resource>", type: 'opus'}, 'fallback.mp3']. BLOB and DATA URIs can be used but only support automatic detection when used in the pair form; otherwise the format must be manually checked before adding the resource.

autoDecode boolean <optional>
true

When using Web Audio the audio files can either be decoded at load time or run-time. Audio files can't be played until they are decoded and, if specified, this enables immediate decoding. Decoding is a non-blocking async process.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 755)

audiosprite(key, urls, jsonURL, jsonData, autoDecode) → {Phaser.Loader}

Add a new audiosprite file to the loader.

Audio Sprites are a combination of audio files and a JSON configuration. The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite

Parameters
Name Type Argument Default Description
key string

Unique asset key of the audio file.

urls Array | string

An array containing the URLs of the audio files, i.e.: [ 'audiosprite.mp3', 'audiosprite.ogg', 'audiosprite.m4a' ] or a single string containing just one URL.

jsonURL string <optional>
null

The URL of the audiosprite configuration JSON object. If you wish to pass the data directly set this parameter to null.

jsonData string | object <optional>
null

A JSON object or string containing the audiosprite configuration data. This is ignored if jsonURL is not null.

autoDecode boolean <optional>
true

When using Web Audio the audio files can either be decoded at load time or run-time. Audio files can't be played until they are decoded and, if specified, this enables immediate decoding. Decoding is a non-blocking async process.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 783)

binary(key, url, callback, callbackContext) → {Phaser.Loader}

Add a 'binary' file to the Loader.

It will be loaded via xhr with a responseType of "arraybuffer". You can specify an optional callback to process the file after load. When the callback is called it will be passed 2 parameters: the key of the file and the file data.

WARNING: If a callback is specified the data will be set to whatever it returns. Always return the data object, even if you didn't modify it.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the binary file.

url string

URL of the binary file.

callback function <optional>
(none)

Optional callback that will be passed the file after loading, so you can perform additional processing on it.

callbackContext object <optional>

The context under which the callback will be applied. If not specified it will use the callback itself as the context.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 702)

bitmapFont(key, textureURL, xmlURL, xmlData, xSpacing, ySpacing) → {Phaser.Loader}

Add a new bitmap font ('bitmapfont') loading request.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the bitmap font.

textureURL string

The url of the font image file.

xmlURL string <optional>

The url of the font data file (xml/fnt)

xmlData object <optional>

An optional XML data object.

xSpacing number <optional>
0

If you'd like to add additional horizontal spacing between the characters then set the pixel value here.

ySpacing number <optional>
0

If you'd like to add additional vertical spacing between the lines then set the pixel value here.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 925)

checkKeyExists(type, key) → {boolean}

Check whether a file/asset with a specific key is queued to be loaded.

To access a loaded asset use Phaser.Cache, eg. Phaser.Cache#checkImageKey

Parameters
Name Type Description
type string

The type asset you want to check.

key string

Key of the asset you want to check.

Returns
boolean -

Return true if exists, otherwise return false.

Source code: loader/Loader.js (Line 330)

getAsset(type, key) → {any}

Find a file/asset with a specific key.

Only assets in the download file queue will be found.

Parameters
Name Type Description
type string

The type asset you want to check.

key string

Key of the asset you want to check.

Returns
any -

Returns an object if found that has 2 properties: index and file; otherwise a non-true value is returned. The index may change and should only be used immediately following this call.

Source code: loader/Loader.js (Line 381)

getAssetIndex(type, key) → {number}

Get the queue-index of the file/asset with a specific key.

Only assets in the download file queue will be found.

Parameters
Name Type Description
type string

The type asset you want to check.

key string

Key of the asset you want to check.

Returns
number -

The index of this key in the filelist, or -1 if not found. The index may change and should only be used immediately following this call

Source code: loader/Loader.js (Line 346)

image(key, url, overwrite) → {Phaser.Loader}

Add an 'image' to the Loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of this image file.

url string

URL of image file.

overwrite boolean <optional>
false

If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 598)

json(key, url, overwrite) → {Phaser.Loader}

Add a 'json' file to the Loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the json file.

url string

URL of the json file.

overwrite boolean <optional>
false

If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 636)

pack(key, url, data, callbackContext) → {Phaser.Loader}

Add a JSON resource pack ('packfile') to the Loader.

Packs are always put before the first non-pack file that is not loaded/loading. This means that all packs added before any loading has started are added to the front of the file/asset list, in order added.

Parameters
Name Type Argument Default Description
key string

Unique asset key of this resource pack.

url string <optional>

URL of the Asset Pack JSON file. If you wish to pass a json object instead set this to null and pass the object as the data parameter.

data object <optional>

The Asset Pack JSON data. Use this to pass in a json data object rather than loading it from a URL. TODO

callbackContext object <optional>
(loader)

Some Loader operations, like Binary and Script require a context for their callbacks. Pass the context here.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 527)

physics(key, url, data, format) → {Phaser.Loader}

Add a new 'physics' data object loading request. If data is supplied the object is loaded immediately.

The data must be in Lime + Corona JSON format. Physics Editor by code'n'web exports in this format natively.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the physics json data.

url string <optional>

The url of the map data file (csv/json)

data object <optional>

An optional JSON data object. If given then the url is ignored and this JSON object is used for physics data instead.

format string <optional>
Phaser.Physics.LIME_CORONA_JSON

The format of the physics data.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 881)

<internal> removeAll()

Remove all file loading requests - this is insufficient to stop current loading. Use reset instead.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 1159)

<internal> removeFile(type, key)

Remove a file/asset from the loading queue.

A file that is loaded or has started loading cannot be removed.

Parameters
Name Type Description
type string

The type of resource to add to the list (image, audio, xml, etc).

key string

Key of the file you want to remove.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 1135)

<internal> replaceInFileList(type, key, url, properties)

Internal function that replaces an existing entry in the file list with a new one. Do not call directly.

Parameters
Name Type Description
type string

The type of resource to add to the list (image, audio, xml, etc).

key string

The unique Cache ID key of this resource.

url string

The URL the asset will be loaded from.

properties object

Any additional properties needed to load the file.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 511)

<internal> reset(hard, clearEvents)

Reset the loader and clear any queued assets. If Loader.resetLocked is true this operation will abort.

This will abort any loading and clear any queued assets.

Optionally you can clear any associated events.

Parameters
Name Type Argument Default Description
hard boolean <optional>
false

If true then the preload sprite and other artifacts may also be cleared.

clearEvents boolean <optional>
false

If true then the all Loader signals will have removeAll called on them.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 405)

<internal> resize()

Called automatically by ScaleManager when the game resizes in RESIZE scalemode.

This can be used to adjust the preloading sprite size, eg.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 313)

script(key, url, callback, callbackContext) → {Phaser.Loader}

Add a JavaScript ('script') file to the Loader.

The loaded JavaScript is automatically turned into a script tag and executed, so be careful what you load!

A callback, which will be invoked as the script tag has been created, can also be specified. The callback must return relevant data.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the script file.

url string

URL of the JavaScript file.

callback function <optional>
(none)

Optional callback that will be called after the script tag has loaded, so you can perform additional processing.

callbackContext object <optional>
(loader)

The context under which the callback will be applied. If not specified it will use the callback itself as the context.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 674)

setPreloadSprite(sprite, direction)

Set a Sprite to be a "preload" sprite by passing it to this method.

A "preload" sprite will have its width or height crop adjusted based on the percentage of the loader in real-time. This allows you to easily make loading bars for games.

The sprite will automatically be made visible when calling this.

Parameters
Name Type Argument Default Description
sprite Phaser.Sprite | Phaser.Image

The sprite or image that will be cropped during the load.

direction number <optional>
0

A value of zero means the sprite will be cropped horizontally, a value of 1 means its will be cropped vertically.

Source code: loader/Loader.js (Line 278)

spritesheet(key, url, frameWidth, frameHeight, frameMax, margin, spacing) → {Phaser.Loader}

Add a new sprite sheet ('spritesheet') to the loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the sheet file.

url string

URL of the sheet file.

frameWidth number

Width of each single frame.

frameHeight number

Height of each single frame.

frameMax number <optional>
-1

How many frames in this sprite sheet. If not specified it will divide the whole image into frames.

margin number <optional>
0

If the frames have been drawn with a margin, specify the amount here.

spacing number <optional>
0

If the frames have been drawn with spacing between them, specify the amount here.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 730)

start()

Start loading the assets. Normally you don't need to call this yourself as the StateManager will do so.

Source code: loader/Loader.js (Line 1172)

text(key, url, overwrite) → {Phaser.Loader}

Add a 'text' file to the Loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the text file.

url string

URL of the text file.

overwrite boolean <optional>
false

If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 617)

tilemap(key, url, data, format) → {Phaser.Loader}

Add a new 'tilemap' loading request. If data is supplied the object is loaded immediately.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the tilemap data.

url string <optional>

The url of the map data file (csv/json)

data object <optional>

An optional JSON data object. If given then the url is ignored and this JSON object is used for map data instead.

format number <optional>
Phaser.Tilemap.CSV

The format of the map data. Either Phaser.Tilemap.CSV or Phaser.Tilemap.TILED_JSON.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 828)

<internal> totalLoadedFiles() → {number}

Returns the number of files that have already been loaded, even if they errored.

Returns
number -

The number of files that have already been loaded (even if they errored)

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 2210)

<internal> totalLoadedPacks() → {number}

Returns the number of asset packs that have already been loaded, even if they errored.

Returns
number -

The number of asset packs that have already been loaded (even if they errored)

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 2236)

<internal> totalQueuedFiles() → {number}

Returns the number of files still waiting to be processed in the load queue. This value decreases as each file in the queue is loaded.

Returns
number -

The number of files that still remain in the load queue.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 2223)

<internal> totalQueuedPacks() → {number}

Returns the number of asset packs still waiting to be processed in the load queue. This value decreases as each pack in the queue is loaded.

Returns
number -

The number of asset packs that still remain in the load queue.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 2249)

<internal> transformUrl()

Transforms the asset URL. The default implementation prepends the baseURL.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Loader.js (Line 1492)

xml(key, url, overwrite) → {Phaser.Loader}

Add an XML ('xml') file to the Loader.

Parameters
Name Type Argument Default Description
key string

Unique asset key of the xml file.

url string

URL of the xml file.

overwrite boolean <optional>
false

If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.

Returns

This Loader instance.

Source code: loader/Loader.js (Line 655)