fps
package com.ultizen.hmlt.utils { import com.ultizen.hmltMiniGame.crystal.greensock.plugins.VolumePlugin; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.utils.Timer; import flash.utils.getTimer; /** * * @author jiading * */ public class FPS extends Sprite { //-------------------------------------------------------------------------- // Static //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Constructor //-------------------------------------------------------------------------- public function FPS() { addEventListener(Event.ADDED_TO_STAGE,addToStagaHandler); } //-------------------------------------------------------------------------- // Variables //-------------------------------------------------------------------------- private var text : TextField; //-------------------------------------------------------------------------- // Properties //-------------------------------------------------------------------------- private var crtTime : uint; private var prevTime : uint; private var fps : uint; private var isWorking : Boolean; private var str : String; //-------------------------------------------------------------------------- // Public Methods //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Event Handler //-------------------------------------------------------------------------- private function addToStagaHandler(event : Event):void { removeEventListener(Event.ADDED_TO_STAGE,addToStagaHandler); isWorking = false; initBg(); initText(); this.buttonMode = true; this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); } private function mouseDownHandler(event : MouseEvent):void { if(!isWorking) { isWorking = true; startWork(); } else { isWorking = false; stopWork(); } } private function efHandler(event : Event):void { crtTime = getTimer(); if(crtTime - 1000 > prevTime) { prevTime = crtTime; text.text = "FPS: "+ fps.toString(); fps = 0; } fps++; } //-------------------------------------------------------------------------- // Protected //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Privates //-------------------------------------------------------------------------- private function initBg():void { this.graphics.beginFill(0x000000); this.graphics.drawRect(0,0,50,20); this.graphics.endFill(); } private function initText():void { text = new TextField(); text.width = 70; text.height = 20; text.selectable = false; text.mouseEnabled = false; text.textColor = 0xff0000; text.text = "show fps"; addChild(text); } private function startWork():void { addEventListener(Event.ENTER_FRAME,efHandler); } private function stopWork():void { removeEventListener(Event.ENTER_FRAME,efHandler); text.text = "show fps"; } //-------------------------------------------------------------------------- // Dispose //-------------------------------------------------------------------------- } }