me.loader

a small class to manage loading of stuff and manage resources There is no constructor function for me.input.

Members

static crossOrigin :String

crossOrigin attribute to configure the CORS requests for Image data element. By default (that is, when the attribute is not specified), CORS is not used at all. The "anonymous" keyword means that there will be no exchange of user credentials via cookies, client-side SSL certificates or HTTP authentication as described in the Terminology section of the CORS specification.

Default Value:
  • undefined
See:
Example
// allow for cross-origin texture loading in WebGL
me.loader.crossOrigin = "anonymous";

// set all ressources to be loaded
me.loader.preload(game.resources, this.loaded.bind(this));

static withCredentials :Boolean

indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.

Default Value:
  • false
See:
Example
// enable withCredentials
me.loader.withCredentials = true;

// set all ressources to be loaded
me.loader.preload(game.resources, this.loaded.bind(this));

Methods

static getBinary(name) → {Object}

return the specified Binary object

Parameters:
Name Type Description
name String

of the binary object ("ymTrack");

Returns:
Object -

requested element or null if not found

static getImage(image) → {HTMLImageElement}

return the specified Image Object

Parameters:
Name Type Description
image String

name of the Image element ("tileset-platformer");

Returns:
HTMLImageElement -

requested element or null if not found

static getJSON(Name) → {Object}

return the specified JSON Object

Parameters:
Name Type Description
Name String

for the json file to load

Returns:
Object

static getLoadProgress() → {Number}

Return the loading progress in percent

Returns:
Number
Deprecated:
  • use callback instead
See:

static getTMX(tmx) → {XML|Object}

return the specified TMX/TSX object

Parameters:
Name Type Description
tmx String

name of the tmx/tsx element ("map1");

Returns:
XML | Object -

requested element or null if not found

static load(resource, onload, onerror)

Load a single resource (to be used if you need to load additional resource during the game)

Parameters:
Name Type Description
resource Object
Properties
Name Type Attributes Description
name String

internal name of the resource

type String

"audio", binary", "image", "json", "tmx", "tsx"

src String

path and/or file name of the resource (for audio assets only the path is required)

stream Boolean <optional>

Set to true to force HTML5 Audio, which allows not to wait for large file to be downloaded before playing.

onload function

function to be called when the resource is loaded

onerror function

function to be called in case of error

Example
// load an image asset
me.loader.load({name: "avatar",  type:"image",  src: "data/avatar.png"}, this.onload.bind(this), this.onerror.bind(this));

// start loading music
me.loader.load({
    name   : "bgmusic",
    type   : "audio",
    src    : "data/audio/"
}, function () {
    me.audio.play("bgmusic");
});

static onload()

onload callback

Example
// set a callback when everything is loaded
me.loader.onload = this.loaded.bind(this);

static onProgress()

onProgress callback
each time a resource is loaded, the loader will fire the specified function, giving the actual progress [0 ... 1], as argument, and an object describing the resource loaded

Example
// set a callback for progress notification
me.loader.onProgress = this.updateProgress.bind(this);

static preload(resources, onloadopt, switchToLoadStateopt)

set all the specified game resources to be preloaded.

Parameters:
Name Type Attributes Default Description
resources Array.<Object>
Properties
Name Type Attributes Description
name String

internal name of the resource

type String

"audio", binary", "image", "json","js", "tmx", "tsx", "fontface"

src String

path and/or file name of the resource (for audio assets only the path is required)

stream Boolean <optional>

Set to true to force HTML5 Audio, which allows not to wait for large file to be downloaded before playing.

onload function <optional>
me.loader.onload

function to be called when all resources are loaded

switchToLoadState boolean <optional>
true

automatically switch to the loading screen

Example
game_resources = [
  // PNG tileset
  {name: "tileset-platformer", type: "image",  src: "data/map/tileset.png"},
  // PNG packed texture
  {name: "texture", type:"image", src: "data/gfx/texture.png"}
  // TSX file
  {name: "meta_tiles", type: "tsx", src: "data/map/meta_tiles.tsx"},
  // TMX level (XML & JSON)
  {name: "map1", type: "tmx", src: "data/map/map1.json"},
  {name: "map2", type: "tmx", src: "data/map/map2.tmx"},
  {name: "map3", type: "tmx", format: "json", data: {"height":15,"layers":[...],"tilewidth":32,"version":1,"width":20}},
  {name: "map4", type: "tmx", format: "xml", data: {xml representation of tmx}},
  // audio resources
  {name: "bgmusic", type: "audio",  src: "data/audio/"},
  {name: "cling",   type: "audio",  src: "data/audio/"},
  // binary file
  {name: "ymTrack", type: "binary", src: "data/audio/main.ym"},
  // JSON file (used for texturePacker)
  {name: "texture", type: "json", src: "data/gfx/texture.json"},
  // JavaScript file
  {name: "plugin", type: "js", src: "data/js/plugin.js"},
  // Font Face
  { name: "'kenpixel'", type: "fontface",  src: "url('data/font/kenvector_future.woff2')" }
];
...
// set all resources to be loaded
me.loader.preload(game.resources, this.loaded.bind(this));

static setBaseURL(type, urlopt)

change the default baseURL for the given asset type.
(this will prepend the asset URL and must finish with a '/')

Parameters:
Name Type Attributes Default Description
type String

"*", "audio", binary", "image", "json", "js", "tmx", "tsx"

url String <optional>
"./"

default base URL

Example
// change the base URL relative address for audio assets
me.loader.setBaseURL("audio", "data/audio/");
// change the base URL absolute address for all object types
me.loader.setBaseURL("*", "http://myurl.com/")

static unload(resource) → {Boolean}

unload specified resource to free memory

Parameters:
Name Type Description
resource Object
Returns:
Boolean -

true if unloaded

Example
me.loader.unload({name: "avatar",  type:"image",  src: "data/avatar.png"});

static unloadAll()

unload all resources to free memory

Example
me.loader.unloadAll();