Game Development Reference
In-Depth Information
Listing 9-3. Preloading Sounds with PreloadJS
var manifest = [
{src:"boom.mp3", id:"boom"},
{src:"crack.mp3", id:"crack"},
{src:"spritesheet.png", id:"ss"},
{src:"ssData.json", id:"ssData"},
];
queue = new createjs.LoadQueue();
queue.installPlugin(createjs.Sound);
createjs.Sound.alternateExtensions = ["ogg"];
queue.loadManifest(queue);
Considering Mobile
Sound on mobile is the one of the major issues game developers have today when targeting devices. There are many
issues with mobile and audio, one of the larger ones is the restriction from playing sound without a user initiating an
event, such as a tap or click event.
This doesn't have to be a deal-breaker in many cases. For example, a play game button will initiate this event,
which can start some background music when your game level begins. Many other game interactivity events and
sounds will work fine as well since mobile games tend to be more touch-centric.
SoundJS takes care of this all as much as possible behind the scenes, depending on the device and browser.
However, the appropriate plug-in must first be installed. To do this, use this convenient method, which will install the
needed plug-in for mobile optimization:
createjs.Sound.initializeDefaultPlugins();
This method prevents the need to set up plug-ins manually. The default plug-ins used are WebAudioPlugin ,
followed by HTMLAudioPlugin .
Many more methods and properties can be used with SoundJS, and can give you the power to create amazing
applications. We'll stick with the needs for most games, which have been explored in this chapter. For more
information on the SoundInstance class, as well as current support and limitations on mobile, visit the extensive
documentation at www.createjs.com/Docs/SoundJS/modules/SoundJS.html .
Before moving on to using sound in a real world example, some organization is needed to properly load and
access all game assets, including sound files within your applications.
Creating an Asset Manager Class
Adding sounds to your games means loading even more assets. Because of this, proper management of these assets is
important. Creating a class to store the ids for these files as properties makes it much easier to access them throughout
the game code. In this section, all asset references, including sounds, will be declared and stored in one class. This
class will also handle the loading and event dispatching of their progress and completion.
Extending the EventDispatcher Class
Because preloading files is a functionality needed in this class, the ability to dispatch events is necessary. The class
will handle the preloading work using PreloadJS, but will not handle any actual messaging to the stage, nor will it
reference properties or call methods from the application that is using it. It merely needs to send messages when
progress on a load has changed and when the load is complete. In order to achieve this, the class will extend from
CreateJS's EventDispatcher class.
 
Search WWH ::




Custom Search