Game Development Reference
In-Depth Information
private var memoryUsedText:String
: The
String
representing the current memory
used value
private var framectrTextField:TextField
: The field to display the
framectrText
String
private var memorypagesField:TextField
: The field to display the
memorypagesText
String
private var frameLast:int
: Contains the
getTimer
milliseconds of the last count event
private var frameCtr:int
: Counts the number of frames that occur between 1,000-
millisecond events
These are the public functions:
public function FrameTimer():void
: The constructor takes in no parameters. Its
function is to set up the text fields for displaying the frame rate counter and memory
usage indicator.
public function countFrames():Boolean
: The
countFrames
function doesn't take in any
parameters, but it returns
true
if 1,000 milliseconds have passed since the last frame
count event. For the
FrameCounter
to work properly,
countFrames
needs to be called at
the end of each game timer, in
Main
or in the
FrameRateProfiler
(or in any class you
created that needs the FPS monitored).
The entire contents of the function are surrounded by a conditional that checks to
see if 1,000 milliseconds (1 second) have passed since that last frame count event.
if (getTimer() >= frameLast + 1000) {
… do all of the frame count event code
return(true);
}else {
frameCtr++;
return(false);
}
If 1,000 milliseconds have not passed since the last frame count event, we simply add 1 to the
frameCtr variable.
What happens when there is a frame count event? This not an actual
Event
in the ActionScript
context but just our name for each time a single second has passed. This event occurs when
1,000 milliseconds, or 1 second, has passed since the last frame count event.
Inside this event we want to display the number of frames that have been counted in the last
second (
frameCtr
) and provide that information to the world outside the class in the
lastframecount
variable.
We also want to display the current amount of memory used and reset the counters for the next
frame count event.
if (showProfiledRate) {
framectrText = frameCtr.toString() + "/" + profiledRate;
}else{
framectrText = frameCtr.toString();
}