AssetManager Class
This class is responsible for loading, unloading and managing the life time of assets. It uses reference counting to ensure that only one instance of a particular asset is loaded at a time. The asset manager will trigger events. "assetLoaded" event is triggered when an asset is downloaded, this event doesn't trigger if an asset's reference count is incremented. "assetUnloaded" event is triggered when an asset's reference count drops to 0 and thus gets unloaded. "errorLoadingAsset" event is triggered when an asset could not be loaded for any reason. "finishedLoading" event is triggered when all asset requests passed in the load function have been processed, for more information see the load function.
Constructor
AssetManager
()
Methods
get
-
assetPath
Parameters:
-
assetPath
StringPath of the asset to be retrieved.
Returns:
load
-
assetPath
Parameters:
-
assetPath
String | Array multipleAn array of strings or one or more strings representing paths of assets to be loaded.
Returns:
Example:
assetManager.load('music.mp3', 'sprite.png');
var array = ['music.mp3', 'sprite.png'];
assetManager.load(array);
subscribe
-
event
-
callback
-
[subscriber]
Parameters:
-
event
StringName of the event being subscribed to.
-
callback
FunctionFunction that will be called back when the specified event is triggered.
-
[subscriber]
Object optionalAn optional parameter that specifies an object that's subscribing. When a function is subscribed with an object, that function will be called back with "this" set to the object.
trigger
-
event
-
[eventObject]
Triggers the event, calling all functions that subscribed to it.
Parameters:
-
event
StringThe event to be triggered.
-
[eventObject]
Object optionalAll functions that were subscribed to the specified event will be called with this as the argument.
unload
-
assetPath
Parameters:
-
assetPath
String or Array multipleAn array of strings or one or more strings representing paths of assets to be unloaded.
Example:
assetManager.unload('music.mp3', 'sprite.png');
var array = ['music.mp3', 'sprite.png'];
assetManager.unload(array);
unsubscribe
-
event
-
[callback]
-
[subscriber]
This is a multi-purpose method. See the example for all the ways it can be overloaded.
Parameters:
-
event
Object -
[callback]
Object optional -
[subscriber]
Object optional
Example:
//Unsubscribing all callbacks for a given event.
eventHandler.unsubscribe('eventName');
//Unsubscribing all callbacks subscribed by the object.
eventHandler.unsubscribe(object);
//Unsubscribing all callbacks an object made for the given event.
eventHandler.unsubscribe('eventName', object);
//Unsubscribing a callback that was subscribed with an object.
eventHandler.unsubscribe('eventName', callback, object);
//Unsubscribing a callback subscribed by itself.
eventHandler.unsubscribe('eventName', callback);
//The last method can't be used to unsubscribe callbacks which were subscribed with an object.
Properties
assetDownloader
AssetDownloader
This is the object that the manager uses to download assets. The manager sets default image and audio types. If those happen to be insufficient, they need to be changed directly in the file.