AS3 FPS 需要自己也会,但这外国佬的代码简明,值得分享给大家。
package  {  
    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);
        //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

文章标签: function class each
个人分类: AS3
想对作者说点什么? 我来说一句



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