您的位置:首页 > 编程语言

AS3 FPS 需要自己也会,但这外国佬的代码简明,值得分享给大家。

2012-03-28 14:33 141 查看
package  {

//imports

import flash.events.Event;
import flash.utils.getTimer;
import flash.display.MovieClip;

public class FPSCalculator extends MovieClip {

//variable to hold the current time
private var currentTime:int = 0;

public function FPSCalculator() {

//add the enter frame listener, this is fired when the SWF updates to a new frame
stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
}

private function onFrameLoop (evt:Event):void{

//for the sanity of the fellow developers, try to put each task into a seperate function.
//this makes it infinitely easier to read for them and yourself on a large project or when you come back to and old one
//since the getTimer() function returns the played time in milliseconds and we want FPSecond, we divide it into 1000
var fps:Number = (1000 / timeDifference);

trace(fps);
}
//this is a get function so it can be referenced just like a variable, without the brackets on the end like a normal function
private function get timeDifference ():int{

//the getTimer() function returns the total played time of the SWF in milliseconds
var totalPlayedTime:int = getTimer();

//The difference in time from the previous frame to this frame will to calculated here
var timeDifference:int = (totalPlayedTime - currentTime);

//The currentTime is set to the total played time so it is ready for the next frame
currentTime = getTimer();

//return the difference in time
return timeDifference
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐