Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Created February 10, 2014 20:41
Show Gist options
  • Save joonjoonjoon/8923774 to your computer and use it in GitHub Desktop.
Save joonjoonjoon/8923774 to your computer and use it in GitHub Desktop.
time-based solution for framerate drop
class GameClass extends FlxGame
{
private var useCustomFpsCounter:Bool = false;
public function new()
{
// Launch your game here
// INIT FRAME COUNTER
Lib.current.stage.addEventListener(Event.ENTER_FRAME, frameRateUpdate);
if (useCustomFpsCounter)
{
framecountertext = new TextField();
framecountertext.defaultTextFormat = new TextFormat(FlxAssets.FONT_DEBUGGER, 20, 0xbbbbbb, false, false, false, null, null, TextFormatAlign.LEFT );
framecountertext.text = "60 - 60";
framecountertext.x = Lib.current.stage.stageWidth/2 - framecountertext.textWidth /2;
framecountertext.y = 60;
framecountertext.width = 200;
Lib.current.stage.addChild(framecountertext);
}
}
public static var elapsedTime:Int; // use this anywhere to compensate for frame drops...
private var myticks:Int;
private var mytotal:Int;
private var timecounter:Int;
private var framecounter:Int;
private var framecountertext:TextField;
private function frameRateUpdate(?FlashEvent:Event):Void
{
myticks = Lib.getTimer();
elapsedTime = myticks - mytotal;
mytotal = myticks;
timecounter += elapsedTime;
framecounter++;
if (useCustomFpsCounter && timecounter > 1000)
{
framecountertext.text = (framecounter -1) + " / " + 60;
framecounter = 0;
timecounter = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment