Game Development Reference
In-Depth Information
We needed to also create an override function for the
systemLevelIn
function that would set the
alpha of the l
evelInScreen
back to
1
at the beginning of each
STATE_SYSTEM_LEVEL_IN
state.
override public function systemLevelIn():void {
levelInScreen.alpha = 1
super.systemLevelIn();
}
Updating the LevelInScreen text
We have added a new message to the
levelInScreen
. This message displays the number of
hearts that need to be collected during the level. To do this we first made sure to import in the
TextField
class:
import flash.text.TextField;
Next, we added the heartsToCollect variable to the variable definition section.
private var heartsToCollect:TextField = new TextField();
In the
init
function, we have set the
defaultTextFormat
of the
heartsToCollect
field to be the
same as the screen text.
heartsToCollect.defaultTextFormat = screenTextFormat;
heartsToCollect.width = 300;
heartsToCollect.x = 50;
heartsToCollect.y = 200;
The
heartsToCollect TextField
also needs to be added to the
levelInScreen
's display list:
levelInScreen.addChild(heartsToCollect);
We also added an event listener into the
init
function that will listen for a new custom event
class called
CustomEventHeartsNeeded
. When this event is dispatched, the number of hearts the
player needs to collect for the level will be sent along with it.
game.addEventListener(CustomEventHeartsNeeded.HEARTS_NEEDED, heartsNeededListener,
false, 0, true);
The
heartsNeededListener
function was also added to
Main.as
:
private function heartsNeededListener(e:CustomEventHeartsNeeded):void {
heartsToCollect.text = "Collect " + e.heartsNeeded + " Hearts";
}
This function will change the
heartsToCollect.text
to the passed in value (from the custom Event)
Increasing the game Frame Rate
As we transition into games that require more and more system resources, we are going to
experiment with using the
Timer
class to create a frame rate different than the Stage frame rate
(
30
).We are going to set the frame rate to
40
for this game. Remember that the
updateAfterEvent
function call inside the
GameFrameWork
's game loop will help smooth out the look of the screen
updates when we choose a frame rate higher than the
Stage
frame rate. In the next chapter, will
we create a new type of timer that will really push the limits of the
Stage
frame rate.
frameRate = 40;