new Loader(game)
Phaser loader constructor. The Loader handles loading all external content such as Images, Sounds, Texture Atlases and data files. It uses a combination of Image() loading and xhr and provides progress and completion callbacks.
Parameters:
| Name | Type | Description |
|---|---|---|
game |
Phaser.Game | A reference to the currently running game. |
- Source:
Members
-
<static, constant> TEXTURE_ATLAS_JSON_ARRAY :number
-
Type:
- number
- Source:
-
<static, constant> TEXTURE_ATLAS_JSON_HASH :number
-
Type:
- number
- Source:
-
<static, constant> TEXTURE_ATLAS_XML_STARLING :number
-
Type:
- number
- Source:
-
baseURL
-
If you want to append a URL before the path of any asset you can set this here. Useful if you need to allow an asset url to be configured outside of the game code. MUST have / on the end of it!
- Source:
Properties:
Name Type Description baseURLstring -
crossOrigin
-
- Source:
Properties:
Name Type Description crossOriginstring The crossOrigin value applied to loaded images
-
game
-
- Source:
Properties:
Name Type Description gamePhaser.Game Local reference to game.
-
hasLoaded
-
- Default Value:
- false
- Source:
Properties:
Name Type Description hasLoadedboolean True if all assets in the queue have finished loading.
-
isLoading
-
- Default Value:
- false
- Source:
Properties:
Name Type Description isLoadingboolean True if the Loader is in the process of loading the queue.
-
onFileComplete
-
- Source:
Properties:
Name Type Description onFileCompletePhaser.Signal Event signal.
-
onFileError
-
- Source:
Properties:
Name Type Description onFileErrorPhaser.Signal Event signal.
-
onLoadComplete
-
- Source:
Properties:
Name Type Description onLoadCompletePhaser.Signal Event signal.
-
onLoadStart
-
- Source:
Properties:
Name Type Description onLoadStartPhaser.Signal Event signal.
-
preloadSprite
-
You can optionally link a sprite to the preloader. If you do so the Sprite's width or height will be cropped based on the percentage loaded.
- Default Value:
- null
- Source:
Properties:
Name Type Description preloadSpriteDescription -
progress
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description progressnumber The Load progress percentage value (from 0 to 100)
Methods
-
<protected> addToFileList(type, key, url, properties)
-
Internal function that adds a new entry to the file list. Do not call directly.
Parameters:
Name Type Description typestring The type of resource to add to the list (image, audio, xml, etc).
keystring The unique Cache ID key of this resource.
urlstring The URL the asset will be loaded from.
propertiesobject Any additional properties needed to load the file.
- Source:
-
atlas(key, textureURL, atlasURL, atlasData, format) → {Phaser.Loader}
-
Add a new texture atlas to the loader.
Parameters:
Name Type Argument Description keystring Unique asset key of the texture atlas file.
textureURLstring The url of the texture atlas image file.
atlasURLstring <optional>
The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
atlasDataobject <optional>
A JSON or XML data object. You don't need this if the data is being loaded from a URL.
formatnumber <optional>
A value describing the format of the data, the default is Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
atlasJSONArray(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Add a new texture atlas to the loader. This atlas uses the JSON Array data format.
Parameters:
Name Type Argument Description keystring Unique asset key of the texture atlas file.
textureURLstring The url of the texture atlas image file.
atlasURLstring <optional>
The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
atlasDataobject <optional>
A JSON or XML data object. You don't need this if the data is being loaded from a URL.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
atlasJSONHash(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Add a new texture atlas to the loader. This atlas uses the JSON Hash data format.
Parameters:
Name Type Argument Description keystring Unique asset key of the texture atlas file.
textureURLstring The url of the texture atlas image file.
atlasURLstring <optional>
The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
atlasDataobject <optional>
A JSON or XML data object. You don't need this if the data is being loaded from a URL.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
atlasXML(key, textureURL, atlasURL, atlasData) → {Phaser.Loader}
-
Add a new texture atlas to the loader. This atlas uses the Starling XML data format.
Parameters:
Name Type Argument Description keystring Unique asset key of the texture atlas file.
textureURLstring The url of the texture atlas image file.
atlasURLstring <optional>
The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
atlasDataobject <optional>
A JSON or XML data object. You don't need this if the data is being loaded from a URL.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
audio(key, urls, autoDecode) → {Phaser.Loader}
-
Add a new audio file to the loader.
Parameters:
Name Type Description keystring Unique asset key of the audio file.
urlsArray | string An array containing the URLs of the audio files, i.e.: [ 'jump.mp3', 'jump.ogg', 'jump.m4a' ] or a single string containing just one URL.
autoDecodeboolean When using Web Audio the audio files can either be decoded at load time or run-time. They can't be played until they are decoded, but this let's you control when that happens. Decoding is a non-blocking async process.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
bitmapFont(key, textureURL, xmlURL, xmlData) → {Phaser.Loader}
-
Add a new bitmap font loading request.
Parameters:
Name Type Argument Description keystring Unique asset key of the bitmap font.
textureURLstring The url of the font image file.
xmlURLstring <optional>
The url of the font data file (xml/fnt)
xmlDataobject <optional>
An optional XML data object.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
checkKeyExists(type, key) → {boolean}
-
Check whether asset exists with a specific key.
Parameters:
Name Type Description typestring The type asset you want to check.
keystring Key of the asset you want to check.
- Source:
Returns:
Return true if exists, otherwise return false.
- Type
- boolean
-
csvLoadComplete(index)
-
Successfully loaded a CSV file.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that loaded.
- Source:
-
dataLoadError(index)
-
Error occured when load a JSON.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that errored.
- Source:
-
fileComplete(index)
-
Called when a file is successfully loaded.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that loaded.
- Source:
-
fileError(index)
-
Error occured when loading a file.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that errored.
- Source:
-
getAsset(type, key) → {any}
-
Gets the asset that is queued for load.
Parameters:
Name Type Description typestring The type asset you want to check.
keystring Key of the asset you want to check.
- Source:
Returns:
Returns an object if found that has 2 properties: index and file. Otherwise false.
- Type
- any
-
image(key, url, overwrite) → {Phaser.Loader}
-
Add an image to the Loader.
Parameters:
Name Type Argument Default Description keystring Unique asset key of this image file.
urlstring URL of image file.
overwriteboolean <optional>
false If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
jsonLoadComplete(index)
-
Successfully loaded a JSON file.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that loaded.
- Source:
-
removeAll()
-
Remove all file loading requests.
- Source:
-
removeFile(type, key)
-
Remove loading request of a file.
Parameters:
Name Type Description typestring The type of resource to add to the list (image, audio, xml, etc).
keystring Key of the file you want to remove.
- Source:
-
<protected> 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 typestring The type of resource to add to the list (image, audio, xml, etc).
keystring The unique Cache ID key of this resource.
urlstring The URL the asset will be loaded from.
propertiesobject Any additional properties needed to load the file.
- Source:
-
reset()
-
Reset loader, this will remove the load queue.
- Source:
-
script(key, url) → {Phaser.Loader}
-
Add a JavaScript file to the Loader. Once loaded the JavaScript file will be automatically turned into a script tag (and executed), so be careful what you load!
Parameters:
Name Type Description keystring Unique asset key of the script file.
urlstring URL of the JavaScript file.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
setPreloadSprite(sprite, direction)
-
You can 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.
Parameters:
Name Type Argument Default Description spritePhaser.Sprite The sprite that will be cropped during the load.
directionnumber <optional>
0 A value of zero means the sprite width will be cropped, a value of 1 means its height will be cropped.
- Source:
-
spritesheet(key, url, frameWidth, frameHeight, frameMax) → {Phaser.Loader}
-
Add a new sprite sheet to the loader.
Parameters:
Name Type Argument Default Description keystring Unique asset key of the sheet file.
urlstring URL of the sheet file.
frameWidthnumber Width of each single frame.
frameHeightnumber Height of each single frame.
frameMaxnumber <optional>
-1 How many frames in this sprite sheet. If not specified it will divide the whole image into frames.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
start()
-
Start loading the assets. Normally you don't need to call this yourself as the StateManager will do so.
- Source:
-
text(key, url, overwrite) → {Phaser.Loader}
-
Add a text file to the Loader.
Parameters:
Name Type Argument Default Description keystring Unique asset key of the text file.
urlstring URL of the text file.
overwriteboolean <optional>
false If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
tilemap(key, mapDataURL, mapData, format) → {Phaser.Loader}
-
Add a new tilemap loading request.
Parameters:
Name Type Argument Default Description keystring Unique asset key of the tilemap data.
mapDataURLstring <optional>
The url of the map data file (csv/json)
mapDataobject <optional>
An optional JSON data object. If given then the mapDataURL is ignored and this JSON object is used for map data instead.
formatstring <optional>
Phaser.Tilemap.CSV The format of the map data. Either Phaser.Tilemap.CSV or Phaser.Tilemap.TILED_JSON.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
tileset(key, url, tileWidth, tileHeight, tileMax, tileMargin, tileSpacing) → {Phaser.Loader}
-
Add a new tile set to the loader. These are used in the rendering of tile maps.
Parameters:
Name Type Argument Default Description keystring Unique asset key of the tileset file.
urlstring URL of the tileset.
tileWidthnumber Width of each single tile in pixels.
tileHeightnumber Height of each single tile in pixels.
tileMaxnumber <optional>
-1 How many tiles in this tileset. If not specified it will divide the whole image into tiles.
tileMarginnumber <optional>
0 If the tiles have been drawn with a margin, specify the amount here.
tileSpacingnumber <optional>
0 If the tiles have been drawn with spacing between them, specify the amount here.
- Source:
Returns:
This Loader instance.
- Type
- Phaser.Loader
-
totalLoadedFiles() → {number}
-
Returns the number of files that have already been loaded, even if they errored.
- Source:
Returns:
The number of files that have already been loaded (even if they errored)
- Type
- number
-
totalQueuedFiles() → {number}
-
Returns the number of files still waiting to be processed in the load queue. This value decreases as each file is in the queue is loaded.
- Source:
Returns:
The number of files that still remain in the load queue.
- Type
- number
-
xmlLoadComplete(index)
-
Successfully loaded an XML file.
Parameters:
Name Type Description indexnumber The index of the file in the file queue that loaded.
- Source: