assload
Modular, extensible asset manager with preloading capabilities.
NOTE: This is experimental software; use at your own risk. (And please report issues)
var assload = loadImages = loadAudio = loader loader music; // Create a new asset manager:loader = ; // Configure the asset manager to use specific loaders// for different asset typesloader; // Create a bundle for our game's essential assets:essentials = loader; // Create a different bundle for non-essential background music:backgroundMusic = loader; // Listen for asset's progressive loading events:essentials; // Listen for asset loading completion events:essentials; // Load the essentials first:essentials;
API
var assload = ;
Create a new asset manager
var loader = ;
Configure an asset manager to load specific asset types
Assload knows nothing about how to load specific types of assets.
You must supply a loader function for any type of asset you wish to load.
loader;
Some existing loaders:
See also: Create a custom loader
Create a bundle of assets to load
var bundle = loader;
Load a bundle of assets
bundle;
bundle.load()
returns a Q promise that
resolves when all assets have completed.
Access loaded assets
Once an asset is loaded, it can be accessed simply with assets.<type>.<name>
.
Prior to loading, the assets.<type>.<name>
is undefined.
For example:
bundle = loader; bundle;
Listen for progress events
If a loader function uses notify
to report partially-loaded asset progress,
asset.progress
events will be fired.
bundle;
Listen for completion events
Whenever a single asset is successfully loaded, an asset.complete
event is fired.
bundle;
Create a custom loader
Loader functions take on the following format:
loader;
whatToLoad
A value passed fromloader.load(...)
; this is usually a filename/uri, but can be any value.
resolve(asset)
A function to call when the asset has successfully loaded.
asset
The loaded asset.
reject(reason)
A function to call when the asset failed to load.
reason
Anew Error
with a message describing why the asset failed to load.
notify(progress)
A function to optionally call periodically as the asset progressively loads.
progress
Value from 0 to 1 representing the percentage of the asset that has loaded.
For a simple example, see how assload-image is written.
License
MIT
Install
npm install assload --save