Game Development Reference
In-Depth Information
Chapter 9
Sounds and Asset Management
Sound effects and music are a crucial part of your games. Adding background music and audible feedback will
instantly increase its polish and overall entertainment value tremendously. This chapter will cover how SoundJS is
used in game development, and how sound restrictions on mobile can be avoided.
Along with playing and controlling sounds, you'll take a deep look at how asset management can help organize
your games to make them easier to update and read. With the inclusion of sound files into your applications, a
self-contained class to handle the ids, loading, and organizing of these sound files, along with other assets, becomes
essential. To handle this, an asset manager class will be built in this chapter and will be used to create a simple game.
SoundJS
SoundJS is extremely easy to use. In fact, it's so easy to use that the brief intro in Chapter 1 covers quite a lot. In this
section, a quick review of SoundJS will be covered, and some more techniques will be introduced, including loading
and mobile considerations.
Using the Sound Class
The Sound class is the API for creating and controlling sounds. A sound can be played directly on the Sound class by
passing in either an id or the full path to the sound file.
createjs.Sound.play("soundID",createjs.Sound.INTERRUPT_NONE,0,0,10,.5,0);
The additional, optional parameters passed into the play method are as follows:
interrupt : How to handle other sounds of the same source currently playing.
delay : The delay in milliseconds before the sound starts.
offset : How far into the sound to begin playback, in milliseconds.
loop : Number of times to loop the audio, -1 for infinite.
volume : The volume of the sound.
pan : The pan of the sound between -1 (left) and 1 (right)
The play method returns an instance of the class, SoundInstance , which can give you more control over when and
what to do with the sound. You can alternatively create an instance without first playing it by using the createInstance
method. Listing 9-1 shows this procedure, along with a few of the methods you can call on a SoundInstance object.
 
Search WWH ::




Custom Search