Game Development Reference
In-Depth Information
Now, let's look at the parameters we have created for playSound() :
soundName : This String contains the sound to play and should be one of the static
constant we created in Main (ie, Main.SOUND_BONUS ).
isSoundTrack : This Boolean defines whether or not we play this sound as a soundtrack
or not. Soundtracks are treated in a special way (see the end of this section).
loops : This int defines the number of times to loop the sound (usually 1 for a single
sound or 10000 for a soundtrack).
offset : This Number defines the offset (in milliseconds) from which the sound will start
playing (usually 0 unless you are using .mp3 files with Flex, then you might need to skip
the leader).
volume : This Number sets the volume at which to play the sound (between 0 and 1 ).
Here is the function definition for playSound() :
public function playSound(soundName:String, isSoundTrack:Boolean=false, loops:int=1,
offset:Number=0, volume:Number=1):void {
This first two lines of code set up the temporary variables for this function: tempSoundTransform
and tempSound . tempSound is simply set to the Sound object stored in the sounds array at the index
specified by the soundName parameter. tempSoundTransform is a bit more complicated. The
SoundTransform object is used to change the volume and pan properties of Sound object. You do
not make changes to volume and pan directly in a Sound object, so you need this object to perform
these functions.
tempSoundTransform.volume=volume;
tempSound = sounds[soundName];
Now, we need to make a decision based on the value of the isSoundTrack parameter passed to
playSound() . If the sound is defined as a soundtrack, we are going to treat it differently than other
sounds. We define a soundtrack as looping music or ambient that will play continuously for a
specified amount of time. We have added this functionality because we find the process of
playing soundtracks, especially when multiple songs will be treated as soundtracks and played at
different times, a bit difficult to manage in AS3. The functionality we are looking for is this: We
only want one soundtrack to play at a time. When a soundtrack is defined as playing, any other
soundtracks playing will stop. This might sound simple, but since we are using a state machine
for our games, the following situation could easily occur:
You are playing soundtrack A at the title screen.
You play soundtrack B while the game is playing.
You play soundtrack C on the end screen.
When the end screen closes, you go back to the title screen and soundtrack A plays
again.
The problem with this scenario is that when the title screen plays the first time, no soundtrack is
playing so we start playing soundtrack A. However, when the title screen displays the second
time (after the end screen is displayed), soundtrack C is already playing, but we need to start
playing soundtrack A again. It's very easy, in this situation, to start playing two soundtracks at
once. You could have some logic to explicitly stop soundtrack C when the end screen is removed,
or when title screen displays, but this can get out of hand as well. As the number of soundtracks
and game states increases so does the number the complications with playing those
Search WWH ::




Custom Search