Game Development Reference
In-Depth Information
The assetsProgress and assetsLoaded methods dispatch the appropriate events by using the two constant
strings that were declared earlier. Now the application that instantiates this object can listen for these events. In the
“Exploding Asteroids” section, you'll see this being applied in a full working example that uses this class. But first,
there is one more method that needs to be created.
Accessing Assets from the AssetManager Class
The assets are all preloaded and ready to use. Next, the getAsset method is created to help access these assets
(see Listing 9-11).
Listing 9-11. The getAsset Function Returns Results from the Load Queue
p.getAsset = function (asset) {
return this.queue.getResult(asset);
}
This method takes a string and returns the result from the load queue. Of course, this queue could be accessed
from the instance of this object, but this makes it a little less verbose when accessing assets. Listing 9-12 shows how
the AssetManager class can be used to access assets for both data and sound.
Listing 9-12. Example of AssetManager in Use
assets = new AssetManager();
assets.on(assets.ASSETS_COMPLETE, assetsComplete);
function assetsComplete(e){
var data = assets.getAsset(assets.GAME_SPRITES_DATA);
spritesheet = new createjs.SpriteSheet(data);
createjs.Sound.play(assets.EXPLODE);
}
A similar, fully working class will be built in the next section, “Exploding Asteroids,” where you will get a chance to
see the full benefits to creating this functionality.
Exploding Asteroids
Exploding Asteroids is a simple game created to put sound effects and music into a working example. The point of this
exercise is to access sounds and other assets using a fully working asset manager class. The same techniques learned
in this chapter will be applied.
Asteroids will eternally fall down the screen at various speeds. The player must destroy them by clicking them,
where they will explode into oblivion. The following is a list of features you wish to accomplish:
Create a class to handle all loading and accessing of assets, including sprite sheet data, the
sprite sheet image, and sounds.
Use the preloader and button components built in Chapter 8.
Create an asteroid sprite class that can play sounds and dispatch an event when it is destroyed.
Click on asteroids to play their explosion animation and explosion sound.
Play background music when the game starts.
No ending or scoring is needed in this exercise.
 
Search WWH ::




Custom Search