Game Development Reference
In-Depth Information
Defining the checkForEndOfGame function
The
checkforEndOfGame
function checks for
gameOver==true
and sends basic custom event to
Main
called
GAME_OVER
. This function is run in each frame tick from the
runGame
method of the
Game
class. The
runGame
function is called from the
SYSYEM_STATE_GAMEPLAY
state of
Main.as
.
private function checkforEndGame():void {
if (gameOver) {
dispatchEvent(new Event(GAME_OVER));
cleanUp();
}
}
The
checkForEndOfGame
function simply checks to see if the
gameOver
variable has been set to
true
. If it has been set, the
cleanup
function is called and dispatches a simple custom
Event
GAME_OVER
.
Main
catches this event and then changes the state to
SYSYEM_STATE_GAMEOVER
. The
gameOver
variable will be set to
false
until the player clicks a red circle or until the
checkForEndOfLevel
function finds that a level has been completed but the
percent
variable is
less that the
percentNeeded
. See the next section for details on the
checkForEndOfLevel
function.
Defining the checkforEndLevel function
The
checkforEndLevel
function handles the level end conditions:
1.
It checks to see if there are no more circles on the screen (
circles.length == 0
) and
if the number of circles created (
numCreated
) equals the number for the level.
2.
If both are
true
, it checks to make sure the user clicked enough to move to next level.
3.
Based on that evaluation, it either sets
gameOver
to
true
or dispatches the simple
custom event
NEW_LEVEL
.
Here is the complete code for the
checkForEndOfLevel
function.
private function checkforEndLevel():void {
if (circles.length == 0 && numCreated == numCircles &&
scoreTexts.length == 0) {
if (percent >= percentNeeded) {
dispatchEvent(new Event(NEW_LEVEL));
}else {
gameOver = true;
}
}
}
If the level is over but the player didn't click enough blue circles to make it to the next level, the
gameOver
variable is set to
true
. If the player did click enough blue circles, a simple custom event
(
NEW_LEVEL
) is dispatched.
Main
listens for this event and changes state to the
SYSYEM_STATE_NEWLEVEL
state.
Defining the cleanUp function
The
cleanUp
function loops through all of the
Circle
instances in the
circles
array. It removes
and disposes of all of them. It is called from the
checkforEndGame
function if the
gameOver
criteria
have been reached. It also removes and cleans up the
scoreTexts
array in the same manner.