一个通用的AS3中监控flash帧数很内存占用的类
在Flash游戏开发的过程中,性能是一个要时时考虑的问题。帧数和内存占用是评价flash运行性能的两个重要指标,因此在测试的过程中,需要能实时观察这两个值的变化情况。
下面的这个类提供了一个通用的方法,只需要将这个类引入到程序中,生成一个该类的对象,便能很方法的的实现上面的功能。
通用的AS3中监控flash帧数很内存占用的类:
1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.Sprite; 6 import flash.events.Event; 7 import flash.geom.Rectangle; 8 import flash.system.System; 9 import flash.text.TextField; 10 import flash.text.TextFieldAutoSize; 11 import flash.text.TextFormat; 12 import flash.utils.getTimer; 13 14 public class Fps extends Sprite 15 { 16 private var fps:TextField; 17 private var mem:TextField; 18 private var instance:Fps; 19 private var bitmapdata:BitmapData; 20 private var i:int = 0; 21 private var n:int = 10; 22 private var diagramTimer:int; 23 private var tfTimer:int; 24 private var skins:int = -1; 25 private var skinsChanged:int = 0; 26 private static const diagramHeight:uint = 40; 27 private static const diagramWidth:uint = 60; 28 private static const maxMemory:uint = 500000000; 29 public function Fps() 30 { 31 this.addEventListener(Event.ADDED_TO_STAGE,init); 32 } 33 34 public function init(e:Event):void 35 { 36 var bitmap:Bitmap = null; 37 this.removeEventListener(Event.ADDED_TO_STAGE,init); 38 fps = new TextField(); 39 mem = new TextField(); 40 if(instance == null) { 41 fps.defaultTextFormat = new TextFormat("Tahoma", 10, 16711680); 42 fps.autoSize = TextFieldAutoSize.LEFT; 43 fps.text = "FPS:" + Number(stage.frameRate).toFixed(2); 44 fps.x = -diagramWidth - 2; 45 addChild(fps); 46 47 mem.defaultTextFormat = new TextFormat("Tahoma", 10, 65280); 48 mem.autoSize = TextFieldAutoSize.LEFT; 49 mem.text = "MEM:" + byteToString(System.totalMemory); 50 mem.x = -diagramWidth - 2; 51 mem.y = fps.y + 10; 52 addChild(mem); 53 bitmapdata = new BitmapData(diagramWidth,diagramHeight,true,255); 54 bitmap = new Bitmap(bitmapdata); 55 bitmap.y = 24; 56 bitmap.x = -diagramWidth; 57 addChildAt(bitmap,0); 58 59 addEventListener(Event.ENTER_FRAME,onEnterFrame); 60 diagramTimer = getTimer(); 61 tfTimer = getTimer(); 62 } 63 } 64 private function onEnterFrame(e:Event):void 65 { 66 i++; 67 if(i >= n) { 68 i = 0; 69 fps.text = "FPS: " + Number(1000 * n / (getTimer() - tfTimer)).toFixed(2); 70 tfTimer = getTimer(); 71 } 72 var _loc_2:* = 1000 / (getTimer() - diagramTimer); 73 var _loc_3:* = _loc_2 > stage.frameRate ? (1) : (_loc_2 / stage.frameRate); 74 diagramTimer = getTimer(); 75 bitmapdata.scroll(1, 0); 76 bitmapdata.fillRect(new Rectangle(0, 0, 1, bitmapdata.height), 2852126720); 77 bitmapdata.setPixel32(0, diagramHeight * (1 - _loc_3), 4294901760); 78 mem.text = "MEM: " + byteToString(System.totalMemory); 79 var ski:int = skins == 0 ? (0) : (skinsChanged / skins); 80 bitmapdata.setPixel32(0, diagramHeight * (1 - ski), 4294967295); 81 var meoryPer:Number = System.totalMemory / maxMemory; 82 bitmapdata.setPixel32(0, diagramHeight * (1 - meoryPer), 4278255360); 83 } 84 85 private function byteToString(byte:uint):String 86 { 87 var byteStr:String = null; 88 if (byte < 1024) 89 { 90 byteStr = String(byte) + "b"; 94. } 91 else if (byte < 10240) 92 { byteStr = Number(byte / 1024).toFixed(2) + "kb"; 93 } else if (byte < 102400) 94 { 95 byteStr = Number(byte / 1024).toFixed(1) + "kb"; 96 } 97 else if (byte < 1048576) { 98 byteStr = Math.round(byte / 1024) + "kb"; 99 } 100 else if (byte < 10485760) 101 { 102 byteStr = Number(byte / 1048576).toFixed(2) + "mb"; 103 } 104 else if (byte < 104857600) 105 { 106 byteStr = Number(byte / 1048576).toFixed(1) + "mb"; 107 } 108 else 109 { 110 byteStr = Math.round(byte / 1048576) + "mb"; 111 } 112 return byteStr; 113 } 114 115 } 116 }
使用方法如下:
1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.Shader; 5 import flash.display.Sprite; 6 import flash.display.StageAlign; 7 import flash.display.StageScaleMode; 8 import flash.events.Event; 9 import flash.events.TimerEvent; 10 import flash.filters.ShaderFilter; 11 import flash.geom.Point; 12 import flash.text.TextField; 13 import flash.utils.Timer; 14 15 import org.osmf.events.TimeEvent; 16 17 public class PixelBenderTest extends Sprite 18 { 19 [Embed(source="kernels/test1.pbj", mimeType="application/octet-stream")] 20 private var ChannelScramblerKernel:Class; 21 [Embed(source="assect/111.png")] 22 private var img:Class; 23 private var shader:Shader ; 24 private var shaderFilter:ShaderFilter 25 private var bitMap:Bitmap; 26 private var angle:Number; 27 private var scal:Number; 28 private var txt:TextField; 29 private var count:int=0 30 31 public function PixelBenderTest() 32 { 33 stage.align = StageAlign.TOP_LEFT; 34 stage.scaleMode = StageScaleMode.NO_SCALE; 35 bitMap = new img() as Bitmap; 36 bitMap.x = 200; 37 bitMap.y = 200; 38 shader = new Shader(new ChannelScramblerKernel()); 39 shader.data.src.input = bitMap.bitmapData; 40 shader.data.center.value = [bitMap.width/2,bitMap.height/2]; 41 angle = 0; 42 scal =1; 43 addEventListener(Event.ENTER_FRAME, onEnterFrame); 44 //增加帧数和内存检测 45 var fps:Fps = new Fps(); 46 fps.x = 100; 47 fps.y = 20; 48 addChild(fps); 49 } 50 51 private function onEnterFrame(e:Event):void{ 52 scal += 0.005; 53 shader.data.radius.value = [angle]; 54 shader.data.scale.value = [scal]; 55 draw(); 56 } 57 58 private function draw():void{ 59 graphics.clear(); 60 graphics.beginShaderFill(shader); 61 graphics.drawRect(0,0,bitMap.width,bitMap.height); 62 graphics.endFill(); 63 } 64 } 65 }
关键是引入这四句代码
//增加帧数和内存检测 var fps:Fps = new Fps(); fps.x = 100; fps.y = 20; addChild(fps);
注意x和y的赋值,这样就可以再flash界面实时的看到帧数和内存的占用情况了