Game Development Reference
In-Depth Information
Notice that
systemTitle
will be called after
systemGameOver
, yet they both play the same soundtrack.
Now, this is why we needed to create special functionality for playing soundtracks in
SoundManager
.
Because the game states in
GameFrameWork
(the class
Main
extends) are independent,
systemTitle
has no idea that
systemGameover
was the last state, nor should it know that information. In fact, there
could be other states in the middle (e.g., show a high score screen and nag for microtransactions).
However, both states need
SoundTrack1
to play, which is why we created the
isSoundTrack
Boolean
parameter for the
SoundTrack.playSoundTrack
function. The function is designed to play only one
soundtrack at a time. Recall, from Chapter 4, that it does this by creating a single
SoundChannel
that
is used to play soundtracks exclusively. Not only does it work for playing the soundtracks, but it
helps keep the separate functions in our state machine from having to know too much specific
information about how the game states flow.
To support playing these soundtracks in
Main
, we need to override
systemTitle
,
systemNewGame
,
and
systemGameOver
. Each of these new function calls
soundManager.playSound
directly and then
calls
super.[function name]
to play its respective namesake function, so that
Main
can operate just
like it has in previous games in this topic.
override public function systemTitle():void {
soundManager.playSound(Main.SOUND_SOUND_TRACK_1,true,1000,0,1);
super.systemTitle();
}
override public function systemNewGame():void {
soundManager.playSound(Main.SOUND_SOUND_TRACK_2,true,1000,0,1);
super.systemNewGame();
}
override public function systemGameOver():void {
soundManager.playSound(Main.SOUND_SOUND_TRACK_1,true,1000,0,1);
super.systemGameOver();
}
Updating the Main.as class
Here is the full code listing for
Main
. We have styled in bold the major changes between this
version and the one in Chapter 8 for Color Drop:
package com.efg.games.dicebattle
{
import com.efg.framework.FrameWorkStates;
import com.efg.framework.GameFrameWork;
import com.efg.framework.BasicScreen;
import com.efg.framework.ScoreBoard;
import com.efg.framework.Game;
import com.efg.framework.SideBySideScoreElement;
import com.efg.framework.SoundManager;
import com.efg.framework.CustomEventSound;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Timer;