Game Development Reference
In-Depth Information
soundtracks. Instead of managing all of this, functionality to simply play one soundtrack at a time
starts looking very useful.
To facilitate this soundtrack functionality, we have created a single SoundChannel object named
soundTrackChannel . If that soundTrackChannel is not null (which means a soundtrack is playing),
we call the stop() function of soundTrackChannel . If it is null, we set soundTrackChannel equal to
the value returned when we start playing the sound. However, this code begs the question, “What
is a SoundChannel ?”
SoundChannel objects in AS3 are used to differentiate sounds that are played, so you can set the
volume and pan attributes separately for each sound. I know this might not sound like much, but
this is a revelation after AS2. In AS2, you were forced to create separate sound objects attached
to separate MovieClip objects to make this work correctly, and the code was still buggy in places.
There were times when, no matter how hard we tried, every sound in a game would be set to the
same volume or worse, no volume at all. With SoundChannel objects, all these limitations go away.
However, the price you pay is slightly more complicated code than in previous versions of Flash.
The good news is this: a SoundChannel is provided for you every time you play a sound. All you
need to do is set its properties once you have a reference to one. In our code, that reference is
stored in the soundChannels array, keyed by the soundName parameter. We store these
SoundChannel objects because, if we don't we will have no way to stop the sound from playing on
request. We will cover this when we discuss the stopSound() function.
if (isSoundTrack) {
if (soundTrackChannel != null) {
soundTrackChannel.stop();
}
soundTrackChannel = tempSound.play(offset,loops);
soundTrackChannel.soundTransform=tempSoundTransform;
}else {
soundChannels[soundName] = tempSound.play(offset, loops);
soundChannels[soundName].soundTransform=tempSoundTransform;
}
}
The stopSound() function is called by Main when it receives a CustomSoundEvent with the type
parameter set to CustomEventSound.STOP_SOUND . The function looks to see if we should treat the
stop as a soundtrack or not and then stops the sound accordingly. If it is a soundtrack, we ignore
the soundName and just call the stop() function of soundTrackChannel . If not, we use soundName as
the index into the soundChannels array to find the correct SoundChannel to stop.
public function stopSound(soundName:String, isSoundTrack:Boolean=false):void {
if (isSoundTrack) {
soundTrackChannel.stop();
}else {
soundChannels[soundName].stop;
}
}
The muteSound() function is used to turn off all the sounds playing in a game. Just like with a
sound channel, we need SoundTransform object to set the volume to mute all sounds. First, we
check the soundMute Boolean. If it is true , we have already muted the sounds, and we will make
them audible again. If it is false , it is time to mute everything. To mute, we set the
muteSoundTransform.volume property to 0 . To unmute, we set the muteSoundTransform.volume
property to 1 . Then, we set the flash.media.SoundMixer.soundTransform static property to the
Search WWH ::




Custom Search