HTML and CSS Reference
In-Depth Information
};
The first definition is the assets hash, defined as an empty object on Q and the simple Q.asset getter
method that just looks up a key in the assets hash. Defining a getter method instead of just exposing the
Q.assets hash directly allows someone to override the method to create, for example, dynamically generated
assets.
The load method is the main method for loading the assets. Its functionality can be broken down into three
sections:
• The first section converts whatever assets the developer wants to load into a consistent format that looks
like the following:
{ "assetname.ext": "assetname.ext",
"assetname2.ext": "assetname2.ext" }
• Next, a loadedCallback is defined. This method is passed as the callback method to each of
the asset-type-specific loading methods. It keeps track of the number of remaining assets and finally
triggers the final callback when everything has loaded correctly. If any errors have been noted, the
callback returns early to prevent the game from trying to start with invalid assets. If a progress call-
back has been provided (such as to show a loading screen with a progress bar), the callback calls it
each time a new asset loads.
• The last section loops over each element in the assets hash, determines the correct type of the asset,
and then dispatches to the appropriate loader function.
This method of asset loading allows developers to create new asset types based on file extensions and add
new loader methods if necessary.
Adding Preload Support
Although the Q.load method does most of the heavy lifting, the engine could make the developer's life easier
by providing preloading support to allow the developer to mark assets for preloading prior to making the actual
call to load. This is useful when putting together scenes or when different modules are responsible for determ-
ining what assets need to be loaded.
Add the code in Listing 10-14 to the regular spot in the bottom of quintus.js before the final return .
Listing 10-14: Preload support
// Array to store any assets that need to be
// preloaded
Q.preloads = [];
// Let us gather assets to load
// and then preload them all at the same time
Q.preload = function(arg,options) {
if(_(arg).isFunction()) {
Q.load(_(Q.preloads).uniq(),arg,options);
Q.preloads = [];
} else {
Q.preloads = Q.preloads.concat(arg);
 
 
Search WWH ::




Custom Search