Game Development Reference
In-Depth Information
muteSoundTransform
: This is a
SoundTransform
object we will use for muting the sounds.
tempSound
: This is a reusable
Sound
object used to hold a sound we want to play.
We use the constructor only to initialize the
sounds
and
soundChannels
arrays:
package com.efg.framework
{
import flash.media.*;
public class SoundManager
{
private var sounds:Array;
private var soundTrackChannel:SoundChannel=new SoundChannel();
private var soundChannels:Array;
private var soundMute:Boolean = false;
private var tempSoundTransform:SoundTransform = new SoundTransform();
private var muteSoundTransform:SoundTransform = new SoundTransform();
private var tempSound:Sound;
public function SoundManager() {
sounds = new Array();
soundChannels = new Array();
}
}
}
We now need a function to add sounds to the sounds array. We will do this with an associative
array. We will use the soundName as the index in the array, and then insert the sound object into
that place in the array. soundName will be the value of a Main.SOUND_XXX static constant that
we will define in the Main class for this game. We will discuss these in the next chapter.
There are several ways to tackle this type of function to add sounds. Since we have chosen to
make the index of the array a
String
, that effectively makes this an associative array.
Alternatively, you could use
int
as the index, as long as it matches the
Main.SOUND_XXX
value for
the sound. We have chosen to use a
String
because we believe it makes the code easier to
understand, but you need to be aware that is certainly not the only way to construct this
functionality. Many modern Flash developers shy away from this type of construct, but we believe
it still has its place.
public function addSound(soundName:String, sound:Sound):void {
sounds[soundName] = sound;
}
First, we initialize the sounds array, and then we start adding sounds into it. Recall that
SoundBonus()
is the class we associated with the
bonus
sound in the Flash library. We then create
six values in our array so that we can reference each
Sound
using the static const index values we
have created for it.
Now, we have to create a way for someone to play one of the sounds in our
sounds
array on
demand. We will do this with the
playSound()
function. In our game framework, this function will
be called when a sound event is dispatched from our game to the
Main
class using the new
CustomEventSound
object (defined in the next section) where the
type
parameter set to
CustomEventSound.PLAY_SOUND
.