Game Development Reference
In-Depth Information
The last task performed in the initialization is resetting the score
displays to their initial state (of 0) and attaching a listener to the
ENTER_FRAME event. In turn, the onEnterFrame handler simply
calls the state machine
s update method, passing it the delta time.
The machine will handle all of the other processing. Compared to
similar ENTER_FRAME loops we
'
ve looked at up to this point, this
is remarkably simple and will keep the logic for the game well
separated. You
'
t compute a value for the delta
time like we have in previous examples. Instead, we make use of a
class called Time with a deltaTime property. This is a custom utility
class that handles calculating these values for us. We
'
ll notice that we don
'
ll look at how
it works shortly, but for now suffice it to say that it is simply return-
ing a number that represents the change in time from the previous
frame in seconds.
Thelastmethodinthechunkofcodewe
'
re examining is the
dispose function. It performs cleanup across the board, calling the
dispose method of all of the game components and removing
the frame loop. When we leave the game, this will be called to free
up memory before moving to another section.
'
private function onPlayer1Score():void
{
mPlayer1Score++;
clipScoreDisplay1.nextFrame();
var overlayClass:Class = Class( getDefinitionByName(
OVERLAY_PLAYER_1_SCORE ) );
mOverlayClip = new overlayClass() as MovieClip;
mOverlayClip.cacheAsBitmap = true;
addChild(mOverlayClip);
mStateMachine.gotoState( STATE_GAME_SCORE );
}
private function onPlayer2Score():void
{
mPlayer2Score++;
clipScoreDisplay2.nextFrame();
var overlayClass:Class = Class( getDefinitionByName(
OVERLAY_PLAYER_2_SCORE ) );
mOverlayClip = new overlayClass() as MovieClip;
mOverlayClip.cacheAsBitmap = true;
addChild(mOverlayClip);
mStateMachine.gotoState( STATE_GAME_SCORE );
}
These two methods are almost identical; they are the callbacks
used for when the engine registers either player scoring a goal.
They display the correct overlay clip, increment that player
s
score, and change the state manager to the GAME_SCORE state.
'
Search Nedrilad ::




Custom Search