Loader Class
The Loader loads and parses different game assets, such as sounds, textures, TMX World files (exported from the Tiled Editor), and Sprite Atlas files (published from Texture Packer).
Constructor
Item Index
Methods
Methods
_dataget
-
file
-
[callback]
Gets a file's data via ajax.
Parameters:
-
file
ObjectThe file descriptor object
-
[callback]
Function optionalThe callback to call once the file has loaded.
fileDone
orfileError
will be called for you.
_getFormatAjaxType
-
type
Returns the ajax type that represents each format type
Parameters:
-
type
ATLAS_FORMAT | FILE_FORMATThe format to get an ajax type for
_loadJsonTilesets
-
file
Loads the tilesets found in a JSON formatted tilemap object.
Parameters:
-
file
ObjectThe file descriptor object
_loadXmlTilesets
-
file
Loads the tilesets found in a XML formatted tilemap object.
Parameters:
-
file
ObjectThe file descriptor object
_onTilesetError
-
file
-
error
Called each time a tileset has an error when loading.
Parameters:
-
file
ObjectThe file descriptor object.
-
error
MixedThe error thrown when loading.
_onTilesetLoaded
-
file
Called each time a tileset is loaded successfully.
Parameters:
-
file
ObjectThe file descriptor object.
add
-
type
-
key
-
url
-
[options]
Adds an asset to be loaded
Parameters:
-
type
StringThe type of asset ot load (image, spritesheet, textureatlas, bitmapfont, tilemap, tileset, audio, etc)
-
key
StringThe unique key of the asset to identify it
-
url
StringThe URL to load the resource from
-
[options]
Object optionalExtra options to apply to the asset, different asset types may require extra options
-
[crossOrigin=false]
Boolean optionalTrue if an image load should be treated as crossOrigin
-
Returns:
Returns itself.
audio
-
key
-
urls
-
[overwrite=false]
Add an audio file to the Loader.
Parameters:
-
key
StringUnique asset key of this image file.
-
urls
ArrayURLs of audio files.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
bitmapFont
-
key
-
textureURL
-
[dataUrl]
-
[data]
-
[format=FILE_FORMAT.XML]
-
[overwrite=false]
Add a bitmap font to the Loader.
Parameters:
-
key
StringUnique asset key of the bitmap font.
-
textureURL
StringThe url of the font image file.
-
[dataUrl]
String optionalThe url of the font data file (xml/fnt)
-
[data]
Object optionalAn optional XML data object (to use instead of loading from a URL)
-
[format=FILE_FORMAT.XML]
FILE_FORMAT optionalThe format of the bitmap font data.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
emit
-
type
-
data
Emits an event which will run all registered listeners for the event type
Parameters:
-
type
StringThe event name to emit
-
data
MixedAny data you want passed along with the event
Returns:
Returns itself.
fileComplete
-
key
Called when a file is successfully loaded.
Parameters:
-
key
StringKey of the successfully loaded file.
fileDone
-
key
-
error
Called when a file is done (error or loaded)
Parameters:
-
key
StringKey of the file done
-
error
MixedThe error that occurred (if there was one)
fileError
-
key
-
error
Error occured when load a file.
Parameters:
-
key
StringKey of the error loading file.
-
error
MixedThe error that was thrown.
getAudioUrl
-
urls
Chooses the audio url to use based on browser support.
Parameters:
-
urls
ArrayAn array of URLs to choose from, chooses the first in the array to be supported by the browser.
Returns:
Returns the URL that was chosen, or undefined
if none are supported.
hasKey
-
key
Check whether asset exists with a specific key.
Parameters:
-
key
StringKey of the asset you want to check.
Returns:
Return true if exists, otherwise return false.
image
-
key
-
url
-
[overwrite=false]
Add an image to the Loader.
Parameters:
-
key
StringUnique asset key of this image file.
-
url
StringURL of image file.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
loadFile
()
Loader
chainable
Loads a single asset from the queued assets in this Loader. To load a single file first queue it by using
one of the methods named for an asset (like audio
, image
, tilemap
, etc.), then call this to load the
first in the queue.
Note: To load the entire queue at once use start
.
Returns:
Returns itself.
off
-
type
-
listener
Removes a listener function for an event type
Parameters:
-
type
StringThe event name to emit
-
listener
FunctionThe function to remove
Returns:
Returns itself.
on
-
type
-
listener
Registers a listener function to be run on an event occurance
Parameters:
-
type
StringThe event name to listen for
-
listener
FunctionThe function to execute when the event happens
Returns:
Returns itself.
once
-
type
-
listener
Registers a one-time callback for an event
Parameters:
-
type
StringThe event name to listen for
-
listener
Functionthe callback to call when the event occurs
Returns:
Returns itself.
reset
()
Loader
chainable
Reset loader, this will remove all loaded assets from the loader's stored list (but not from the cache).
Returns:
Returns itself.
spritesheet
-
key
-
url
-
frameWidth
-
frameHeight
-
numFrames
-
[overwrite=false]
Add a sprite sheet image to the Loader.
Parameters:
-
key
StringUnique asset key of this image file.
-
url
StringURL of image file.
-
frameWidth
NumberWidth of each single frame.
-
frameHeight
NumberHeight of each single frame.
-
numFrames
NumberHow many frames in this sprite sheet.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
start
()
Loader
chainable
Starts the loading of all the assets that are queued to load
Returns:
Returns itself.
text
-
key
-
url
-
[overwrite=false]
Add a text file to the Loader.
Parameters:
-
key
StringUnique asset key of this image file.
-
url
StringURL of image file.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
tilemap
-
key
-
url
-
[data]
-
[format=FILE_FORMAT.JSON]
-
[overwrite=false]
Add a tilemap to the Loader.
Parameters:
-
key
StringUnique asset key of the tilemap data.
-
url
StringThe url of the map data file (csv/json/xml)
-
[data]
String | Object optionalThe data for the map, (to use instead of loading from a URL)
-
[format=FILE_FORMAT.JSON]
Number optionalThe format of the map data.
-
[overwrite=false]
Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
Properties
assets
Array
The array of asset keys
assets
Array
The asset data
baseUrl
String
The base URL to prepend to a url, requires the trailing slash
crossOrigin
String
The cross origin value for loading images
done
Number
Number of assets done to load (for progress)
isLoading
Boolean
Whether the loader is actively loading the assets
isLoading
Boolean
Whether the loader has finished loading
progress
Number
The progress of the loader (0 - 100)
total
Number
Number of assets total to load
Events
complete
Fired when all the assets have loaded
error
Fired if a loader encounters an error
Event Payload:
-
error
MixedThe error that occured when loading
-
key
StringThe key for the asset that was being loaded
progress
Fired when an item has loaded
Event Payload:
-
progress
NumberThe integer progress value, between 0 and 100.
start
Fired when an item has started loading
Event Payload:
-
numAssets
NumberThe number of assets that are going to be loaded