Game Development Reference
In-Depth Information
muteSoundTransform
object we just configured. This goes the heart of Flash's ability to play
sounds and turns the volume all the way up or down. We also set the
soundMute
to
true
or
false
,
depending on which part if the
if
statement we dropped into, so that the next time this function is
called we will know what to do.
public function muteSound():void {
if (soundMute) {
soundMute=false;
muteSoundTransform.volume=1;
flash.media.SoundMixer.soundTransform=muteSoundTransform;
}else{
muteSoundTransform.volume=0;
flash.media.SoundMixer.soundTransform=muteSoundTransform;
soundMute=true;
}
}
And that is it for the
SoundManager
class. I know this was more complicated than it should be to
simply play a sound, but we will reuse (and improve) this class later in this topic, so we had to get
it out of the way early.
However, we are not finished. We still need to create the custom event class that we will use to
play sounds and stop sounds. The good news is that this event is very much like the custom
events we created in Chapter 2. The differences are that we have two different types of events
(
PLAY_SOUND
and
STOP_SOUND
) and we require a different set or parameters to be set when the
event is dispatched:
name
: This
String
is the
Main.SOUND_XXX
static const that represents the sound we want
to play. We will define our const variables in the next chapter.
loops
: This
int
defines the number of times to loop the sound. For a soundtrack, this
should be something very high (i.e.,
10000
).
offset
: This
Number
defines the millisecond offset at which to start the sound playing.
volume
: This
Number
determines the volume to play the sound; it's value is between 0
and 1.
isSoundTrack
: If this
Boolean
is true,
SoundManager
will treat this as a soundtrack and will
use the special
SoundTrack
channel to play it.
Here we will start the definition for the
CustomEventSound
class:
package com.efg.framework
{
import flash.events.*;
public class CustomEventSound extends Event
{
public static const PLAY_SOUND:String = "playsound";
public static const STOP_SOUND:String = "stopsound";
public var name:String;
public var loops:int;
public var offset:Number;
public var volume:Number;