Game Development Reference
In-Depth Information
The playSound method is the largest and most important in the
entire class. It checks to find whether the sound requested has ever
been played (created) before. If it hasn
t, the getDefinitionByName
method is used to look up the sound by name in the library.
If a custom ApplicationDomain was specified (which we
ll further
discuss in Chapter 14), it attempts to look up the sound name in
that domain instead. We found this to be necessary, when loading
asset SWF files with sounds embedded in them, so this is a consider-
able improvement in flexibility over the original version. If the sound
cannot be found, the assumption is made that an external file was
requested, and the sound uses the name as the URL to load the
sound. Once the sound engine object has been created, listeners are
attached to it to be notified, when the sound completes or is
stopped. An additional listener is also added if the sound is in an
external file and loading it fails. The two additional protected
methods, onIOError and onSoundEvent, are for dispatching events
to anything listening to the engine. The stopSound method does
what you would expect; it stops the sound passed in for the name
parameter. However, we
if no sound
name is passed in, the engine will stop all the sounds. During a
game, you might often need to kill every sound that
ve added an extra feature
s playing, and
this prevents you from having to name them individually.
Next, we move on to the volume, pan, and transform methods:
public function setVolume(value:Number, name:String = null):void {
if (name) {
if (_soundList[name]) {
_soundList[name].volume = Math.max
(0, Math.min(1, value));
} else {
throw new Error( " Sound " + name + "
does not exist. " );
} else {
for (var i:String in _soundList) _soundList[i].
volume = Math.max(0, Math.min(1, value));
public function getVolume(name:String):Number {
if (_soundList[name]) {
return _soundList[name].volume;
} else {
throw new Error( " Sound " +name+ " does not exist. " );
return 0;
Search Nedrilad ::

Custom Search