关闭

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

1633人阅读 评论(0) 收藏 举报
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
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论