Game Development Reference
In-Depth Information
Lastly, and most importantly, you use the third argument to set the scope to call the callback function in. By
passing in this ( Game ) , it will remain in scope when the animation is complete. This can be very important in many
programming situations.
if i've lost you, don't worry. we'll get back into encapsulating code and scope as you build your games later on
in the topic. this approach is an important one when dealing with game programming in JavaScript, so i felt it necessary
to briefly cover it as applied to animations using tweenJS.
Note
You can see how powerful and easy-to-use TweenJS can be. In fact, you could actually get pretty far with EaselJS
and TweenJS alone when making games, but there a few more tools you need to check out that will polish your games
and help them perform more reliably.
SoundJS
One of the biggest gripes about HTML5 when it comes to games is audio. Browsers all have their own ways of dealing
with audio. There is heavy fragmentation when it comes to file types, playback control, volume, and a few other
annoyances that make HTML5 games that much more difficult to master. And it all gets worse when you move
to mobile.
SoundJS makes an effort to bridge these gaps and it does a pretty good job at doing so. It prevents you from doing
endless conditionals each time you want to handle a sound object and provides the concept of ids to reference these
loaded audio files for quick access and playback control. A simple demo will show you the ease in working with audio
with SoundJS.
createjs.Sound.registerSound("audio/boom.mp3", "boom",5);
var boom = createjs.Sound.play("boom");
The first thing you need to do with a sound file is to register it. This is essential for working with any sound asset.
This puts a reference to the sound into memory and allows you to assign an id for quick access to it when you need it,
regardless of scope in your game. The third parameter is optional and specifies how many concurrently playing
instances of the same sound can be played. Once your sound is registered, you can play it by passing in the registered
id string to the play method.
Although these two lines are about all you need to play a sound, it is important that your sounds are loaded
before attempting to access them. If a sound has even the slightest delay, especially in games, your application will
appear off or even broken. You can preload and register all sounds using PreloadJS, which you'll see in action in the
next section. But before you move on, let's take a look at a few more features of SoundJS.
Events
There are a handful of useful events that will trigger from a SoundJS instance. These can come in handy when you
need to know information about the sound in question, such as when it's loaded, ready to play, finished, and so on.
The following is an example of how to listen for when the sound file has finished playing:
mySound.addEventListener ("complete", function(mySound) {
alert('sound has finished')
});
 
 
Search WWH ::




Custom Search