Game Development Reference
In-Depth Information
This function shows how we can share a single listener for the OK button click on three different
screens (title, instructions, and game over). We switch on the
id
value passed from
CUSTOMEVENT_OK_CLICKED
.
No matter which screen we were on, the last line of the function calls the
switchSystemState
function and passes in the
nextSystemState
variable.
The systemInstructions function definition
The
systemInstructions
function is used to display the instructions screen to the user. It is
associated with the
STATE_SYSTEM_INSTRUCTIONS
state. It is called a single time and then
processing is passed to the
STATE_SYSYEM_WAIT_FOR_CLOSE
state.
The
systemInstructions
function is very similar to the
systemTitle
function. In fact, they are
almost identical with a few minor changes. We first add the
systemInstructionsScreen
to the
displayList
with an
addChild
call.
We also setup a listener for the same
CustomEvent
that the
titleScreen
used and we switch to
the
STATE_SYSTEM_WAIT_FOR_CLOSE
state. Again, this state does nothing but let the system wait for
the
CustomEventButtonId.BUTTON_ID
on an instance of the
BasicScreen
class. Finally, we switch
to the
STATE_SYSYEM_WAIT_FOR_CLOSE
on to wait for the OK button to be clicked. We also set the
nextSystemState
to be evaluated and used once the OK button is clicked.
The systemGameOver function definition
The
systemGameOver
function displays the
gameOverScreen
. It is associated with the
STATE_SYSTEM_GAMEOVER
state and waits for the OK button click shared with the other
BasicScreen
instances.
We take a look at the
systemGameover
function now, because it uses the
BasicScreen
instance
gameOverScreen
in a similar manner as the
titleScreen
and
instructionsScreen
instances. The
systemGameOver
state is set in Main when the
Game
class instance sends out the simple custom
event called
GAME_OVER
. In the next section, we will see the
Main
class set up to listen for this
event from the
Game
class.
The
sytemGameOver
function follows the exact same format as the
systemTitle
and
systemInstructions
functions. There is one difference though: it takes care of removing the
Game
class instance,
game
, from the display list with a call to the
removeChild
function.
The systemNewGame function definition
The
systemNewGame
function is associated with the
STATE_SYSTEM_NEW_GAME
state. It is called one
time and then moves on to the
STATE_SYSTEM_NEW_LEVEL
state. Its purpose is to add all of the
event listeners for communication between the
Game
class and some other framework classes
(such as the
ScoreBoard
class). It also calls the
game.newGame
function to allow the
Game
instance
to do its own internal new game related processing.
When setting up a new game, we first add our
Game
class instance (
game
) to the display list with
the
addChild
function call. This will display the
Game.as
Sprite (or
MovieClip
) on the screen. Next,
we set up some basic communication between the
Game.as
and the
Main.as
classes. We do this
by creating four event lsteners. The first two we set up are custom event class instances (classes
we will discuss in detail later in the chapter).