Skip to content
Jesper Sørensen edited this page Sep 7, 2014 · 1 revision

Class: Loader

Engine. Loader

Class for loading and storing resources. On engine startup a Loader object is instantiated to the global variable "loader". This loader object will also create a load overlay (the overlay saying "jsEngine loading"), this overlay will not be removed until the loader.hideOverlay() is called.

new Loader()

Constructor for the Loader class. This function will also create a load overlay which will not disappear until the hideOverlay is called. Therefore, remember to call hideOverlay, when your game is ready to be shown.

Source:

Methods

<private> checkAllLoaded() → {boolean}

Checks if all resources - of all themes - has been loaded. This check is automatically called any time a single resource has finished loading.

Source:
Returns:

Whether or not all themes' resources has been successfully loaded

Type
boolean

generateMask(resourceString, alphaLimit) → {HTMLCanvasElement}

Generates a mask for an image specified by its resource string. This function is used by getMask to fetch and cache masks for each of the loaded images.

Parameters:
Name Type Description
resourceString string

A resource string specifying the image to generate a mask for

alphaLimit number

An alpha value (0-255). Pixel having this alpha value or larger will become black on the mask, pixels with a value below the limit will become completely transparent

Source:
Returns:

A canvas element with the generated mask

Type
HTMLCanvasElement

getAllMusic() → {string[]}

Fetches all loaded music tracks' resource strings (from all themes).

Source:
Returns:

An array containing all loaded music tracks' resource strings

Type
string[]

getAllSounds() → {string[]}

Fetches all loaded sounds' resource strings (from all themes).

Source:
Returns:

An array containing all loaded sounds' resource strings

Type
string[]

getImage(resource, themeName) → {HTMLImageElement}

Fetches an image from the Loader. This function will automatically be called by objects that implements the Sprite object.

Parameters:
Name Type Description
resource string

The resource string of the image that should be fetched

themeName string

The name of the theme from which the image should be fetched. If unset, the engine's default theme will be used

Source:
Returns:

The image element corresponding to the resource string and theme

Type
HTMLImageElement

getImageSources() → {string[]}

Fetches all loaded images' resource strings (from all themes).

Source:
Returns:

An array containing all loaded images' resource strings

Type
string[]

getMask(resource, themeName) → {HTMLCanvasElement}

Creates (or loads from cache) a mask for an image which is loaded by the Loader. This function is automatically used by the Collidable object for fetching masks for collision checking.

Parameters:
Name Type Description
resource string

The resource string of the image that should be fetched

themeName string

The name of the theme from which the image should be fetched. If unset, the engine's default theme will be used

Source:
Returns:

A generated mask (canvas element) for the image element corresponding to the resource string and theme

Type
HTMLCanvasElement

getMusic(resource, themeName) → {Sound.Music}

Fetches a music track from the Loader.

Parameters:
Name Type Description
resource string

The resource string of the track that should be fetched

themeName string

The name of the theme from which the track should be fetched. If unset, the engine's default theme will be used

Source:
Returns:

A Music object corresponding to the resource string and theme

Type
Sound.Music

<private> getResource(resource, typeString, themeName) → {HTMLImageElement|Sound|Music}

Fetches a resource from the Loader's cache. Used by the getImage-, getSound- and getMusic functions.

Parameters:
Name Type Description
resource string

The resource string of the resource that should be fetched

typeString string

A string representing the resource type, possible values are: "image", "sfx" and "music"

themeName string

The name of the theme from which the image should be fetched. If unset, the engine's default theme will be used

Source:
Returns:

The resource corresponding to the provided resource string, resource type and theme name

Type
HTMLImageElement | Sound | Music

getSound(resource, themeName) → {Sound.Effect}

Fetches a sound from the Loader.

Parameters:
Name Type Description
resource string

The resource string of the sound that should be fetched

themeName string

The name of the theme from which the sound should be fetched. If unset, the engine's default theme will be used

Source:
Returns:

A Sound object corresponding to the resource string and theme

Type
Sound.Effect

hideOverlay(callback)

Fades out the load overlay (which is automatically created by Loader's constructor). Remember to call this function, when your game is ready to be shown. Otherwise, the load overlay will never disappear.

Parameters:
Name Type Description
callback function

A callback function to run when the overlay has finished fading out

Source:

loadClasses(paths) → {boolean}

Loads JavaScript classes from files. The loaded classes' names must follow the following format: [ClassName].js

Parameters:
Name Type Description
paths string[]

An array of paths to JavaScripts - containing classes - which should be loaded

Source:
Returns:

True, when the classes has been loaded without any errors

Type
boolean

loadExternalResource(resourceString, path, typeString, onLoaded, options)

Loads an external resource to the built in External-theme

Parameters:
Name Type Argument Default Description
resourceString string

The proposed resource string of the resource when loaded

path string

The path to the resource's file

typeString string <optional>
"images"

A string defining the type of resource, can be: "images" (image file), sfx (sound effects) or "music" (background music)

onLoaded function <optional>
function () {}

A function to call when the resource has been loaded

options string <optional>
""

A string defining the resource options (same as the string used for defining animations in a theme file)

Source:

<private> loadResources(theme, object, typeString)

Loads resources to a theme. This function is used by loadThemes for caching the theme resources.

Parameters:
Name Type Description
theme Object

A theme object to load the resources to

object Object

An object containing references to theme resources (like the subcategories of theme files)

typeString string

A string defining the resource type. Supported types are: "images", "sfx" and "music"

Source:

<private> loadThemes(themeNames, callback)

Loads a list of themes. This function is automatically called by the Engine during its startup, for loading the themes specified by the launch options.

Parameters:
Name Type Description
themeNames string[]

An array of theme names (as strings) to load

callback function

A callback function to run when all the themes has been loaded

Source:

reloadAllClasses()

Reloads all classes. This function is very useful for applying code changes without having to refresh the browser, usually it has to be run multiple times though, to force the browser not to just load the files from its cache.

Source:

Generated with wicked.
Clone this wiki locally