·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3.0 统计与计算文件FPS信息代码的示例

时间:2012-10-30 22:39cnblogs.com
[AS3]AS3.0 统计与计算文件FPS信息代码的示例
  1. package { 
  2.         import flash.display.*; 
  3.         import flash.events.*; 
  4.         import flash.geom.*; 
  5.         import flash.system.*; 
  6.         import flash.text.*; 
  7.         import flash.utils.*; 
  8.  
  9.         public class FPS extends Sprite { 
  10.                 private var currentY:int; 
  11.                 private var diagramTimer:int; 
  12.                 private var tfTimer:int; 
  13.                 private var diagram:BitmapData; 
  14.                 private var mem:TextField; 
  15.                 private var fps:TextField; 
  16.                 private var tfDelay:int = 0
  17.                 static private  const maxMemory:uint = 4.1943e+007; 
  18.                 static private  const diagramWidth:uint = 60
  19.                 static private  const tfDelayMax:int = 10
  20.                 static private  var instance:FPS; 
  21.                 static private  const diagramHeight:uint = 40
  22.                 public function FPS():void { 
  23.                         this.addEventListener(Event.ADDED_TO_STAGE,run); 
  24.                 } 
  25.  
  26.                 private function run(e:Event):void { 
  27.                         if (instance == null) { 
  28.                                 instance=this
  29.                                 fps = new TextField(); 
  30.                                 mem = new TextField(); 
  31.                                 this.mouseEnabled = false
  32.                                 this.mouseChildren = false
  33.                                 fps.defaultTextFormat = new TextFormat("Tahoma", 10, 13421772); 
  34.                                 fps.autoSize = TextFieldAutoSize.LEFT; 
  35.                                 fps.text = "FPS: " + Number(stage.frameRate).toFixed(2); 
  36.                                 fps.selectable = false
  37.                                 fps.x = -diagramWidth - 2; 
  38.                                 addChild(fps); 
  39.                                 mem.defaultTextFormat = new TextFormat("Tahoma", 10, 13421568); 
  40.                                 mem.autoSize = TextFieldAutoSize.LEFT; 
  41.                                 mem.text = "MEM: " + bytesToString(System.totalMemory); 
  42.                                 mem.selectable = false
  43.                                 mem.x = -diagramWidth - 2; 
  44.                                 mem.y = 10
  45.                                 addChild(mem); 
  46.                                 currentY = 20
  47.                                 diagram = new BitmapData(diagramWidth, diagramHeight, true, 0x20ffff00); 
  48.                                 var _loc_2:Bitmap = new Bitmap(diagram); 
  49.                                 _loc_2.y = currentY + 4; 
  50.                                 _loc_2.x = -diagramWidth; 
  51.                                 addChildAt(_loc_2, 0); 
  52.                                 this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
  53.                                 this.stage.addEventListener(Event.RESIZE, onResize); 
  54.                                 onResize(); 
  55.                                 diagramTimer = getTimer(); 
  56.                                 tfTimer = getTimer(); 
  57.                         } 
  58.                 } 
  59.                 private function bytesToString(memory:uint):String { 
  60.                         var _str:String; 
  61.                         if (memory < 1024) { 
  62.                                 _str = String(memory) + "b"; 
  63.                         } else if (memory < 10240) { 
  64.                                 _str = Number(memory / 1024).toFixed(2) + "kb"; 
  65.                         } else if (memory < 102400) { 
  66.                                 _str = Number(memory / 1024).toFixed(1) + "kb"; 
  67.                         } else if (memory < 1048576) { 
  68.                                 _str =int(memory / 1024) + "kb"; 
  69.                         } else if (memory < 10485760) { 
  70.                                 _str = Number(memory / 1048576).toFixed(2) + "mb"; 
  71.                         } else if (memory < 104857600) { 
  72.                                 _str = Number(memory / 1048576).toFixed(1) + "mb"; 
  73.                         } else { 
  74.                                 _str = int(memory / 1048576) + "mb"; 
  75.                         } 
  76.                         return _str; 
  77.                 } 
  78.  
  79.                 private function onEnterFrame(e:Event):void { 
  80.                         tfDelay++; 
  81.                         if (tfDelay >= tfDelayMax) { 
  82.                                 tfDelay = 0
  83.                                 fps.text = "FPS: " + Number(1000 * tfDelayMax / (getTimer() - tfTimer)).toFixed(2); 
  84.                                 tfTimer = getTimer(); 
  85.                         } 
  86.                         var _loc_2:* = 1000 / (getTimer() - diagramTimer); 
  87.                         var _loc_3:* = _loc_2 > stage.frameRate ? (1) : (_loc_2 / stage.frameRate); 
  88.                         diagramTimer = getTimer(); 
  89.                         diagram.scroll(1, 0); 
  90.                         diagram.fillRect(new Rectangle(0, 0, 1, diagramHeight), 0x20ffff00); 
  91.                         diagram.setPixel32(0, diagramHeight * (1 - _loc_3), 0xffcccccc); 
  92.                         mem.text = "MEM: " + bytesToString(System.totalMemory); 
  93.                         var _loc_5:Number = System.totalMemory / maxMemory; 
  94.                         diagram.setPixel32(0, diagramHeight * (1 - _loc_5), 0xffff6600); 
  95.                 } 
  96.                 private function onResize(e:Event = null):void { 
  97.                         var _point:Point = parent.globalToLocal(new Point(80, 90));//stage.stageWidth  
  98.                         this.x = _point.x; 
  99.                         this.y = _point.y; 
  100.                 } 
  101.         } 

[AS3]AS3.0 统计与计算文件FPS信息代码的示例

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器