API Docs for: v0.1.0
Show:

Loader Class

Extends Object

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

Loader

(
  • game
)

Parameters:

  • game Game

    Game instance this belongs to

Methods

_dataget

(
  • file
  • [callback]
)
private

Gets a file's data via ajax.

Parameters:

  • file Object

    The file descriptor object

  • [callback] Function optional

    The callback to call once the file has loaded. fileDone or fileError will be called for you.

_getFormatAjaxType

(
  • type
)
private

Returns the ajax type that represents each format type

Parameters:

_loadJsonTilesets

(
  • file
)
private

Loads the tilesets found in a JSON formatted tilemap object.

Parameters:

  • file Object

    The file descriptor object

_loadXmlTilesets

(
  • file
)
private

Loads the tilesets found in a XML formatted tilemap object.

Parameters:

  • file Object

    The file descriptor object

_onTilesetError

(
  • file
  • error
)
private

Called each time a tileset has an error when loading.

Parameters:

  • file Object

    The file descriptor object.

  • error Mixed

    The error thrown when loading.

_onTilesetLoaded

(
  • file
)
private

Called each time a tileset is loaded successfully.

Parameters:

  • file Object

    The file descriptor object.

add

(
  • type
  • key
  • url
  • [options]
)
Loader chainable

Adds an asset to be loaded

Parameters:

  • type String

    The type of asset ot load (image, spritesheet, textureatlas, bitmapfont, tilemap, tileset, audio, etc)

  • key String

    The unique key of the asset to identify it

  • url String

    The URL to load the resource from

  • [options] Object optional

    Extra options to apply to the asset, different asset types may require extra options

    • [crossOrigin=false] Boolean optional

      True if an image load should be treated as crossOrigin

Returns:

Loader:

Returns itself.

audio

(
  • key
  • urls
  • [overwrite=false]
)
Loader chainable

Add an audio file to the Loader.

Parameters:

  • key String

    Unique asset key of this image file.

  • urls Array

    URLs of audio files.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

Returns itself.

bitmapFont

(
  • key
  • textureURL
  • [dataUrl]
  • [data]
  • [format=FILE_FORMAT.XML]
  • [overwrite=false]
)
Loader chainable

Add a bitmap font to the Loader.

Parameters:

  • key String

    Unique asset key of the bitmap font.

  • textureURL String

    The url of the font image file.

  • [dataUrl] String optional

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

  • [data] Object optional

    An optional XML data object (to use instead of loading from a URL)

  • [format=FILE_FORMAT.XML] FILE_FORMAT optional

    The format of the bitmap font data.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

Returns itself.

emit

(
  • type
  • data
)
Mixed chainable

Emits an event which will run all registered listeners for the event type

Parameters:

  • type String

    The event name to emit

  • data Mixed

    Any data you want passed along with the event

Returns:

Mixed:

Returns itself.

fileComplete

(
  • key
)
private

Called when a file is successfully loaded.

Parameters:

  • key String

    Key of the successfully loaded file.

fileDone

(
  • key
  • error
)
private

Called when a file is done (error or loaded)

Parameters:

  • key String

    Key of the file done

  • error Mixed

    The error that occurred (if there was one)

fileError

(
  • key
  • error
)
private

Error occured when load a file.

Parameters:

  • key String

    Key of the error loading file.

  • error Mixed

    The error that was thrown.

getAudioUrl

(
  • urls
)
String

Chooses the audio url to use based on browser support.

Parameters:

  • urls Array

    An array of URLs to choose from, chooses the first in the array to be supported by the browser.

Returns:

String:

Returns the URL that was chosen, or undefined if none are supported.

hasKey

(
  • key
)
Boolean

Check whether asset exists with a specific key.

Parameters:

  • key String

    Key of the asset you want to check.

Returns:

Boolean:

Return true if exists, otherwise return false.

image

(
  • key
  • url
  • [overwrite=false]
)
Loader chainable

Add an image to the Loader.

Parameters:

  • key String

    Unique asset key of this image file.

  • url String

    URL of image file.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

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:

Loader:

Returns itself.

off

(
  • type
  • listener
)
Mixed chainable

Removes a listener function for an event type

Parameters:

  • type String

    The event name to emit

  • listener Function

    The function to remove

Returns:

Mixed:

Returns itself.

on

(
  • type
  • listener
)
Mixed chainable

Registers a listener function to be run on an event occurance

Parameters:

  • type String

    The event name to listen for

  • listener Function

    The function to execute when the event happens

Returns:

Mixed:

Returns itself.

once

(
  • type
  • listener
)
Mixed chainable

Registers a one-time callback for an event

Parameters:

  • type String

    The event name to listen for

  • listener Function

    the callback to call when the event occurs

Returns:

Mixed:

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:

Loader:

Returns itself.

spritesheet

(
  • key
  • url
  • frameWidth
  • frameHeight
  • numFrames
  • [overwrite=false]
)
Loader chainable

Add a sprite sheet image to the Loader.

Parameters:

  • key String

    Unique asset key of this image file.

  • url String

    URL of image file.

  • frameWidth Number

    Width of each single frame.

  • frameHeight Number

    Height of each single frame.

  • numFrames Number

    How many frames in this sprite sheet.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

Returns itself.

start

() Loader chainable

Starts the loading of all the assets that are queued to load

Returns:

Loader:

Returns itself.

text

(
  • key
  • url
  • [overwrite=false]
)
Loader chainable

Add a text file to the Loader.

Parameters:

  • key String

    Unique asset key of this image file.

  • url String

    URL of image file.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

Returns itself.

tilemap

(
  • key
  • url
  • [data]
  • [format=FILE_FORMAT.JSON]
  • [overwrite=false]
)
Loader chainable

Add a tilemap to the Loader.

Parameters:

  • key String

    Unique asset key of the tilemap data.

  • url String

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

  • [data] String | Object optional

    The data for the map, (to use instead of loading from a URL)

  • [format=FILE_FORMAT.JSON] Number optional

    The format of the map data.

  • [overwrite=false] Boolean optional

    If an entry with a matching key already exists this will over-write it.

Returns:

Loader:

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)

game

Game

The game instance this loader belongs to

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 Mixed

    The error that occured when loading

  • key String

    The key for the asset that was being loaded

progress

Fired when an item has loaded

Event Payload:

  • progress Number

    The integer progress value, between 0 and 100.

start

Fired when an item has started loading

Event Payload:

  • numAssets Number

    The number of assets that are going to be loaded