HTML and CSS Reference
In-Depth Information
Armed with the mime type of an extension, it can check if a file of the supported type is playable by calling
Audio.canPlayType with the mime type.
The reason for all these file-type gymnastics is that there isn't a single audio format supported across all
browsers. As such, it falls onto the developers to make sure they provide audio files in as many formats as
are necessary for the supported browsers. In general this means making audio assets available in .mp3 and
.ogg format. To make the developer's life easier, the code in the preceding listing strips off the extension
of any audio file and tries to find a file in one of formats the developers have indicated support for in
Q.options.audioSupported that is also supported by the browser.
If no supported audio is available, the engine just acts like everything is still okay and lets the user play the
game without audio. This means that if developers want to support only one audio format such as .mp3 , they
can do so and still have the game play, albeit without sound.
Each of the preceding methods also takes an errorCallback that is triggered if something were to go
wrong with the loading of the asset.
Finishing the Loader
To round out the basic loader functionality, the engine needs a spot to keep track of assets along with a method
to do the dirty work of synchronizing the loading of assets.
Before getting to this, though, you need to add some defaults to the engine to make loading assets more suc-
cinct and provide a list of supported audio formats for your game. Near the top of the Quintus definition, you
defined an options hash that can hold any defaults for the engine. Update the hash to match the code in Listing
10-12 .
Listing 10-12: Default options for Quintus
var Quintus = function(opts) {
var Q = {};
Q.options = {
imagePath: "images/",
audioPath: "audio/",
dataPath: "data/",
audioSupported: [ 'mp3','ogg' ],
sound: true
if(opts) { _(Q.options).extend(opts); }
Assets are held in the system in a simple object and indexed by name. To look up a hash, you just need to
call Q.asset(name) . The actual dirty work for loading a list of assets is handled by the Q.load method.
Its duty is to take whatever the programmer passed in for assets to load (it could be a string, an array, or an
object), turn it into a consistent data structure, and then loop over each of the objects, calling the appropriate
loader method (as defined in Listing 10-12 ) .
Add the code in Listing 10-13 to the usual spot in quintus.js before the final return .
Listing 10-13: Asset loading
// Return a name without an extension
Q._removeExtension = function(filename) {
return filename.replace(/\.(\w{3,4})$/,"");
Search WWH ::

Custom Search