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 - loader/Loader.js, line 22
Members
-
<static, constant> PHYSICS_LIME_CORONA_JSON :number
-
- Source - loader/Loader.js, line 299
-
<static, constant> PHYSICS_PHASER_JSON :number
-
- Source - loader/Loader.js, line 305
-
<static, constant> TEXTURE_ATLAS_JSON_ARRAY :number
-
- Source - loader/Loader.js, line 281
-
<static, constant> TEXTURE_ATLAS_JSON_HASH :number
-
- Source - loader/Loader.js, line 287
-
<static, constant> TEXTURE_ATLAS_JSON_PYXEL :number
-
- Source - loader/Loader.js, line 311
-
<static, constant> TEXTURE_ATLAS_XML_STARLING :number
-
- Source - loader/Loader.js, line 293
-
_withSyncPointDepth
-
A counter: if more than zero, files will be automatically added as a synchronization point.
- Source - loader/Loader.js, line 204
Properties:
Name Type Description _withSyncPointDepth;
integer -
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 - loader/Loader.js, line 83
-
<internal> cache :Phaser.Cache
-
Local reference to the Phaser.Cache.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - loader/Loader.js, line 36
-
crossOrigin :boolean|string
-
The crossOrigin value applied to loaded images. Very often this needs to be set to 'anonymous'.
Type:
- boolean | string
- Source - loader/Loader.js, line 74
-
enableParallel :boolean
-
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 - loader/Loader.js, line 188
-
<internal> game :Phaser.Game
-
Local reference to game.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - loader/Loader.js, line 29
-
hasLoaded :boolean
-
True if all assets in the queue have finished loading.
- Source - loader/Loader.js, line 57
-
isLoading :boolean
-
True if the Loader is in the process of loading the queue.
- Source - loader/Loader.js, line 50
-
<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 - loader/Loader.js, line 198
-
onFileComplete :Phaser.Signal
-
This event is dispatched when a file has either loaded or failed to load.
Any function bound to this will receive the following parameters:
progress, file key, success?, total loaded files, total files
Where progress is a number between 1 and 100 (inclusive) representing the percentage of the load.
- Source - loader/Loader.js, line 152
-
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 beforeonPackComplete
.Params:
(file key, file)
- Source - loader/Loader.js, line 163
-
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 - loader/Loader.js, line 139
-
onLoadComplete :Phaser.Signal
-
This event is dispatched when the final file in the load queue has either loaded or failed.
- Source - loader/Loader.js, line 118
-
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 - loader/Loader.js, line 111
-
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 - loader/Loader.js, line 129
-
path :string
-
The value of
path
, if set, is placed before any relative file path given. For example:load.path = "images/sprites/"; load.image("ball", "ball.png"); load.image("tree", "level1/oaktree.png"); load.image("boom", "http://server.com/explode.png");
Would load the
ball
file fromimages/sprites/ball.png
and the tree fromimages/sprites/level1/oaktree.png
but the fileboom
would load from the URL given as it's an absolute URL.Please note that the path is added before the filename but after the baseURL (if set.)
The string must end with a "/".
- Source - loader/Loader.js, line 103
-
<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 - loader/Loader.js, line 67
-
progress
-
The rounded load progress percentage value (from 0 to 100). See Phaser.Loader#progressFloat.
- Source - loader/Loader.js, line 3023
Properties:
Type Description integer -
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.- Source - loader/Loader.js, line 3005
Properties:
Type Description number -
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.
- Source - loader/Loader.js, line 43
-
useXDomainRequest :boolean
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- This is only relevant for IE 9.
- Source - loader/Loader.js, line 173
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.
Methods
-
addSyncPoint(type, key) → {Phaser.Loader}
-
Add a synchronization point to a specific file/asset in the load queue.
This has no effect on already loaded assets.
Parameters:
Name Type Description type
string The type of resource to turn into a sync point (image, audio, xml, etc).
key
string Key of the file you want to turn into a sync point.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1648
- See:
-
- withSyncPoint
-
<internal> addToFileList(type, key, url, properties, overwrite, extension) → {Phaser.Loader}
-
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 <optional>
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 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.
extension
string <optional>
If no URL is given the Loader will sometimes auto-generate the URL based on the key, using this as the extension.
Returns:
This instance of the Phaser Loader.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - loader/Loader.js, line 492
-
atlas(key, textureURL, atlasURL, atlasData, format) → {Phaser.Loader}
-
Adds a Texture Atlas file to the current load queue.
To create the Texture Atlas you can use tools such as:
If using Texture Packer we recommend you enable "Trim sprite names". If your atlas software has an option to "rotate" the resulting frames, you must disable it.
You can choose to either load the data externally, by providing a URL to a json file. Or you can pass in a JSON object or String via the
atlasData
parameter. If you pass a String the data is automatically run throughJSON.parse
and then immediately added to the Phaser.Cache.If URLs are provided the files are not loaded immediately after calling this method, but are added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getImage(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the JSON file as needed.The URLs can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the textureURL isn't specified then the Loader will take the key and create a filename from that. For example if the key is "player" and textureURL is null then the Loader will set the URL to be "player.png". The same is true for the atlasURL. If atlasURL isn't specified and no atlasData has been provided then the Loader will set the atlasURL to be the key. For example if the key is "player" the atlasURL will be set to "player.json".
If you do not desire this action then provide URLs and / or a data object.
Parameters:
Name Type Argument Description key
string Unique asset key of the texture atlas file.
textureURL
string <optional>
URL of the texture atlas image file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
URL of the texture atlas data file. If undefined or
null
and no atlasData is given, the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".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>
The format of the data. Can be Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY (the default), Phaser.Loader.TEXTURE_ATLAS_JSON_HASH or Phaser.Loader.TEXTURE_ATLAS_XML_STARLING.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1517
-
atlasJSONArray(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Adds a Texture Atlas file to the current load queue.
Unlike
Loader.atlasJSONHash
this call expects the atlas data to be in a JSON Array format.To create the Texture Atlas you can use tools such as:
If using Texture Packer we recommend you enable "Trim sprite names". If your atlas software has an option to "rotate" the resulting frames, you must disable it.
You can choose to either load the data externally, by providing a URL to a json file. Or you can pass in a JSON object or String via the
atlasData
parameter. If you pass a String the data is automatically run throughJSON.parse
and then immediately added to the Phaser.Cache.If URLs are provided the files are not loaded immediately after calling this method, but are added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getImage(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the JSON file as needed.The URLs can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the textureURL isn't specified then the Loader will take the key and create a filename from that. For example if the key is "player" and textureURL is null then the Loader will set the URL to be "player.png". The same is true for the atlasURL. If atlasURL isn't specified and no atlasData has been provided then the Loader will set the atlasURL to be the key. For example if the key is "player" the atlasURL will be set to "player.json".
If you do not desire this action then provide URLs and / or a data object.
Parameters:
Name Type Argument Description key
string Unique asset key of the texture atlas file.
textureURL
string <optional>
URL of the texture atlas image file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
URL of the texture atlas data file. If undefined or
null
and no atlasData is given, the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".atlasData
object <optional>
A JSON data object. You don't need this if the data is being loaded from a URL.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1371
-
atlasJSONHash(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Adds a Texture Atlas file to the current load queue.
Unlike
Loader.atlas
this call expects the atlas data to be in a JSON Hash format.To create the Texture Atlas you can use tools such as:
If using Texture Packer we recommend you enable "Trim sprite names". If your atlas software has an option to "rotate" the resulting frames, you must disable it.
You can choose to either load the data externally, by providing a URL to a json file. Or you can pass in a JSON object or String via the
atlasData
parameter. If you pass a String the data is automatically run throughJSON.parse
and then immediately added to the Phaser.Cache.If URLs are provided the files are not loaded immediately after calling this method, but are added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getImage(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the JSON file as needed.The URLs can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the textureURL isn't specified then the Loader will take the key and create a filename from that. For example if the key is "player" and textureURL is null then the Loader will set the URL to be "player.png". The same is true for the atlasURL. If atlasURL isn't specified and no atlasData has been provided then the Loader will set the atlasURL to be the key. For example if the key is "player" the atlasURL will be set to "player.json".
If you do not desire this action then provide URLs and / or a data object.
Parameters:
Name Type Argument Description key
string Unique asset key of the texture atlas file.
textureURL
string <optional>
URL of the texture atlas image file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
URL of the texture atlas data file. If undefined or
null
and no atlasData is given, the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".atlasData
object <optional>
A JSON data object. You don't need this if the data is being loaded from a URL.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1417
-
atlasXML(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Adds a Texture Atlas file to the current load queue.
This call expects the atlas data to be in the Starling XML data format.
To create the Texture Atlas you can use tools such as:
If using Texture Packer we recommend you enable "Trim sprite names". If your atlas software has an option to "rotate" the resulting frames, you must disable it.
You can choose to either load the data externally, by providing a URL to an xml file. Or you can pass in an XML object or String via the
atlasData
parameter. If you pass a String the data is automatically run throughLoader.parseXML
and then immediately added to the Phaser.Cache.If URLs are provided the files are not loaded immediately after calling this method, but are added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getImage(key)
. XML files are automatically parsed upon load. If you need to control when the XML is parsed then useLoader.text
instead and parse the XML file as needed.The URLs can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the textureURL isn't specified then the Loader will take the key and create a filename from that. For example if the key is "player" and textureURL is null then the Loader will set the URL to be "player.png". The same is true for the atlasURL. If atlasURL isn't specified and no atlasData has been provided then the Loader will set the atlasURL to be the key. For example if the key is "player" the atlasURL will be set to "player.xml".
If you do not desire this action then provide URLs and / or a data object.
Parameters:
Name Type Argument Description key
string Unique asset key of the texture atlas file.
textureURL
string <optional>
URL of the texture atlas image file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".atlasURL
string <optional>
URL of the texture atlas data file. If undefined or
null
and no atlasData is given, the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.xml".atlasData
object <optional>
An XML data object. You don't need this if the data is being loaded from a URL.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1463
-
audio(key, urls, autoDecode) → {Phaser.Loader}
-
Adds an audio file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getSound(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio. When they try to play the audio becomes extremely distorted and buzzes, eventually crashing the sound system. The solution is to use a lower encoding rate such as 44100 Hz.
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, however it consumes huge amounts of CPU time on mobiles especially.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 968
-
audiosprite(key, urls, jsonURL, jsonData, autoDecode) → {Phaser.Loader}
-
A legacy alias for Loader.audioSprite. Please see that method for documentation.
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, however it consumes huge amounts of CPU time on mobiles especially.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1070
-
audiosprite(key, urls, jsonURL, jsonData, autoDecode) → {Phaser.Loader}
-
Adds an audio sprite file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
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
Retrieve the file via
Cache.getSoundData(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.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, however it consumes huge amounts of CPU time on mobiles especially.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1011
-
binary(key, url, callback, callbackContext) → {Phaser.Loader}
-
Adds a binary file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getBinary(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.bin". It will always add
.bin
as the extension. If you do not desire this action then provide a URL.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 <optional>
URL of the binary file. If undefined or
null
the url will be set to<key>.bin
, i.e. ifkey
was "alien" then the URL will be "alien.bin".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 - loader/Loader.js, line 888
-
bitmapFont(key, textureURL, atlasURL, atlasData, xSpacing, ySpacing) → {Phaser.Loader}
-
Adds Bitmap Font files to the current load queue.
To create the Bitmap Font files you can use:
BMFont (Windows, free): http://www.angelcode.com/products/bmfont/ Glyph Designer (OS X, commercial): http://www.71squared.com/en/glyphdesigner Littera (Web-based, free): http://kvazars.com/littera/
You can choose to either load the data externally, by providing a URL to an xml file. Or you can pass in an XML object or String via the
xmlData
parameter. If you pass a String the data is automatically run throughLoader.parseXML
and then immediately added to the Phaser.Cache.If URLs are provided the files are not loaded immediately after calling this method, but are added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getBitmapFont(key)
. XML files are automatically parsed upon load. If you need to control when the XML is parsed then useLoader.text
instead and parse the XML file as needed.The URLs can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the textureURL isn't specified then the Loader will take the key and create a filename from that. For example if the key is "megaFont" and textureURL is null then the Loader will set the URL to be "megaFont.png". The same is true for the atlasURL. If atlasURL isn't specified and no atlasData has been provided then the Loader will set the atlasURL to be the key. For example if the key is "megaFont" the atlasURL will be set to "megaFont.xml".
If you do not desire this action then provide URLs and / or a data object.
Parameters:
Name Type Argument Default Description key
string Unique asset key of the bitmap font.
textureURL
string URL of the Bitmap Font texture file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "megaFont" then the URL will be "megaFont.png".atlasURL
string URL of the Bitmap Font atlas file (xml/json). If undefined or
null
ANDatlasData
is null, the url will be set to<key>.xml
, i.e. ifkey
was "megaFont" then the URL will be "megaFont.xml".atlasData
object An optional Bitmap Font atlas in string form (stringified xml/json).
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 - loader/Loader.js, line 1280
-
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 - loader/Loader.js, line 367
-
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
andfile
; otherwise a non-true value is returned. The index may change and should only be used immediately following this call.- Source - loader/Loader.js, line 418
-
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 - loader/Loader.js, line 383
-
image(key, url, overwrite) → {Phaser.Loader}
-
Adds an Image to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the image via
Cache.getImage(key)
The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of this image file.
url
string <optional>
URL of an image file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".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 - loader/Loader.js, line 670
-
images(keys, urls) → {Phaser.Loader}
-
Adds an array of images to the current load queue.
It works by passing each element of the array to the Loader.image method.
The files are not loaded immediately after calling this method. The files are added to the queue ready to be loaded when the loader starts.
Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle.
The keys must be unique Strings. They are used to add the files to the Phaser.Cache upon successful load.
Retrieve the images via
Cache.getImage(key)
The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Description keys
array An array of unique asset keys of the image files.
urls
array <optional>
Optional array of URLs. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png". If provided the URLs array length must match the keys array length.Returns:
This Loader instance.
- Source - loader/Loader.js, line 699
-
json(key, url, overwrite) → {Phaser.Loader}
-
Adds a JSON file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getJSON(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the text file as needed.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.json". It will always add
.json
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of the json file.
url
string <optional>
URL of the JSON file. If undefined or
null
the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".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 - loader/Loader.js, line 771
-
pack(key, url, data, callbackContext) → {Phaser.Loader}
-
Add a JSON resource pack ('packfile') to the Loader.
A packfile is a JSON file that contains a list of assets to the be loaded. Please see the example 'loader/asset pack' in the Phaser Examples repository.
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 queue, in the order added.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
The URL of the packfile can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.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 - loader/Loader.js, line 590
-
physics(key, url, data, format) → {Phaser.Loader}
-
Adds a physics data file to the current load queue.
The data must be in
Lime + Corona
JSON format. Physics Editor by code'n'web exports in this format natively.You can choose to either load the data externally, by providing a URL to a json file. Or you can pass in a JSON object or String via the
data
parameter. If you pass a String the data is automatically run throughJSON.parse
and then immediately added to the Phaser.Cache.If a URL is provided the file is not loaded immediately after calling this method, but is added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getJSON(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the text file as needed.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified and no data is given then the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL or data is given then the Loader will set the URL to be "alien.json". It will always use
.json
as the extension.If you do not desire this action then provide a URL or data object.
Parameters:
Name Type Argument Default Description key
string Unique asset key of the physics json data.
url
string <optional>
URL of the physics data file. If undefined or
null
and no data is given the url will be set to<key>.json
, i.e. ifkey
was "alien" then the URL will be "alien.json".data
object | string <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 - loader/Loader.js, line 1219
-
<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 - loader/Loader.js, line 1695
-
<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 - loader/Loader.js, line 1671
-
<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 - loader/Loader.js, line 574
-
<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 - loader/Loader.js, line 442
-
<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 - loader/Loader.js, line 350
-
script(key, url, callback, callbackContext) → {Phaser.Loader}
-
Adds a JavaScript file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String.
The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.js". It will always add
.js
as the extension. If you do not desire this action then provide a URL.Upon successful load the 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 <optional>
URL of the JavaScript file. If undefined or
null
the url will be set to<key>.js
, i.e. ifkey
was "alien" then the URL will be "alien.js".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 Phaser Loader as the context.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 853
-
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 - loader/Loader.js, line 315
-
shader(key, url, overwrite) → {Phaser.Loader}
-
Adds a fragment shader file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getShader(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "blur" and no URL is given then the Loader will set the URL to be "blur.frag". It will always add
.frag
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of the fragment file.
url
string <optional>
URL of the fragment file. If undefined or
null
the url will be set to<key>.frag
, i.e. ifkey
was "blur" then the URL will be "blur.frag".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 - loader/Loader.js, line 799
-
spritesheet(key, url, frameWidth, frameHeight, frameMax, margin, spacing) → {Phaser.Loader}
-
Adds a Sprite Sheet to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
To clarify the terminology that Phaser uses: A Sprite Sheet is an image containing frames, usually of an animation, that are all equal dimensions and often in sequence. For example if the frame size is 32x32 then every frame in the sprite sheet will be that size. Sometimes (outside of Phaser) the term "sprite sheet" is used to refer to a texture atlas. A Texture Atlas works by packing together images as best it can, using whatever frame sizes it likes, often with cropping and trimming the frames in the process. Software such as Texture Packer, Flash CC or Shoebox all generate texture atlases, not sprite sheets. If you've got an atlas then use
Loader.atlas
instead.The key must be a unique String. It is used to add the image to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getImage(key)
. Sprite sheets, being image based, live in the same Cache as all other Images.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.png". It will always add
.png
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of the sheet file.
url
string URL of the sprite sheet file. If undefined or
null
the url will be set to<key>.png
, i.e. ifkey
was "alien" then the URL will be "alien.png".frameWidth
number Width in pixels of a single frame in the sprite sheet.
frameHeight
number Height in pixels of a single frame in the sprite sheet.
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 - loader/Loader.js, line 926
-
start()
-
Start loading the assets. Normally you don't need to call this yourself as the StateManager will do so.
- Source - loader/Loader.js, line 1708
-
text(key, url, overwrite) → {Phaser.Loader}
-
Adds a Text file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getText(key)
The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.txt". It will always add
.txt
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of the text file.
url
string <optional>
URL of the text file. If undefined or
null
the url will be set to<key>.txt
, i.e. ifkey
was "alien" then the URL will be "alien.txt".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 - loader/Loader.js, line 744
-
tilemap(key, url, data, format) → {Phaser.Loader}
-
Adds a Tile Map data file to the current load queue.
You can choose to either load the data externally, by providing a URL to a json file. Or you can pass in a JSON object or String via the
data
parameter. If you pass a String the data is automatically run throughJSON.parse
and then immediately added to the Phaser.Cache.If a URL is provided the file is not loaded immediately after calling this method, but is added to the load queue.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getTilemapData(key)
. JSON files are automatically parsed upon load. If you need to control when the JSON is parsed then useLoader.text
instead and parse the text file as needed.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified and no data is given then the Loader will take the key and create a filename from that. For example if the key is "level1" and no URL or data is given then the Loader will set the URL to be "level1.json". If you set the format to be Tilemap.CSV it will set the URL to be "level1.csv" instead.
If you do not desire this action then provide a URL or data object.
Parameters:
Name Type Argument Default Description key
string Unique asset key of the tilemap data.
url
string <optional>
URL of the tile map file. If undefined or
null
and no data is given the url will be set to<key>.json
, i.e. ifkey
was "level1" then the URL will be "level1.json".data
object | string <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 - loader/Loader.js, line 1142
-
<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 - loader/Loader.js, line 2951
-
<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 - loader/Loader.js, line 2977
-
<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 - loader/Loader.js, line 2964
-
<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 - loader/Loader.js, line 2990
-
<internal> transformUrl(url, file) → {string}
-
Transforms the asset URL.
The default implementation prepends the baseURL if the url doesn't begin with http or //
Parameters:
Name Type Description url
string The url to transform.
file
object The file object being transformed.
Returns:
string -The transformed url. In rare cases where the url isn't specified it will return false instead.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - loader/Loader.js, line 2036
-
video(key, urls, loadEvent, asBlob) → {Phaser.Loader}
-
Adds a video file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getVideo(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.You don't need to preload a video in order to play it in your game. See
Video.createVideoFromURL
for details.Parameters:
Name Type Argument Default Description key
string Unique asset key of the video 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:"boom.mp4"
,['boom.mp4', 'boom.ogg', 'boom.webm']
, or[{uri: "data:<opus_resource>", type: 'opus'}, 'fallback.mp4']
. 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.loadEvent
string <optional>
'canplaythrough' This sets the Video source event to listen for before the load is considered complete. 'canplaythrough' implies the video has downloaded enough, and bandwidth is high enough that it can be played to completion. 'canplay' implies the video has downloaded enough to start playing, but not necessarily to finish. 'loadeddata' just makes sure that the video meta data and first frame have downloaded. Phaser uses this value automatically if the browser is detected as being Firefox and no
loadEvent
is given, otherwise it defaults tocanplaythrough
.asBlob
boolean <optional>
false Video files can either be loaded via the creation of a video element which has its src property set. Or they can be loaded via xhr, stored as binary data in memory and then converted to a Blob. This isn't supported in IE9 or Android 2. If you need to have the same video playing at different times across multiple Sprites then you need to load it as a Blob.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1088
-
withSyncPoints(callback, callbackContext) → {Phaser.Loader}
-
Add a synchronization point to the assets/files added within the supplied callback.
A synchronization point denotes that an asset must be completely loaded before subsequent assets can be loaded. An asset marked as a sync-point does not need to wait for previous assets to load (unless they are sync-points). Resources, such as packs, may still be downloaded around sync-points, as long as they do not finalize loading.
Parameters:
Name Type Argument Default Description callback
function The callback is invoked and is supplied with a single argument: the loader.
callbackContext
object <optional>
(loader) Context for the callback.
Returns:
This Loader instance.
- Source - loader/Loader.js, line 1622
-
xml(key, url, overwrite) → {Phaser.Loader}
-
Adds an XML file to the current load queue.
The file is not loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts.
The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load.
Retrieve the file via
Cache.getXML(key)
.The URL can be relative or absolute. If the URL is relative the
Loader.baseURL
andLoader.path
values will be prepended to it.If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" and no URL is given then the Loader will set the URL to be "alien.xml". It will always add
.xml
as the extension. If you do not desire this action then provide a URL.Parameters:
Name Type Argument Default Description key
string Unique asset key of the xml file.
url
string <optional>
URL of the XML file. If undefined or
null
the url will be set to<key>.xml
, i.e. ifkey
was "alien" then the URL will be "alien.xml".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 - loader/Loader.js, line 826