Flash AS3定时器非常关闭

时间:2010-10-21 08:54:00

标签: flash actionscript-3 timer

我正在使用Flash CS4和AS3。 我想要一个定时器以50 ms的间隔调用一个函数100次。然而,计时器需要的时间比它应该的长得多,在100次重复后,它会增加1677毫秒(1.677秒!)。我在这里遗漏了什么或计时器是不准确的吗?

代码

function test(event:TimerEvent):void{
   trace("GetTimer(): " + getTimer() + " || Timer.currentCount: " + _timer.currentCount);
}

var _timer:Timer = new Timer(50, 100); 
_timer.addEventListener(TimerEvent.TIMER, test); 
_timer.start();

跟踪输出:

GetTimer():74 || Timer.currentCount:1

GetTimer():140 || Timer.currentCount:2

GetTimer():209 || Timer.currentCount:3

GetTimer():275 || Timer.currentCount:4

GetTimer():340 || Timer.currentCount:5

GetTimer():407 || Timer.currentCount:6

GetTimer():476 || Timer.currentCount:7

GetTimer():542 || Timer.currentCount:8

GetTimer():608 || Timer.currentCount:9

GetTimer():677 || Timer.currentCount:10

...

GetTimer():3340 || Timer.currentCount:50

...

GetTimer():6677 || Timer.currentCount:100

感谢您的帮助。

问候,

克里斯

4 个答案:

答案 0 :(得分:8)

请勿使用Timer这么短的间隔。 Flash中的时间并不是一个简单的主题,请参阅this开始。要测量50 ms,我建议使用getTimer()函数和ENTER_FRAME事件来检查时间间隔是否已经过去。

答案 1 :(得分:1)

很多因素都会影响Timer的准确性。 SWF帧速率,其他过程,基本上是电影的一般环境。

答案 2 :(得分:0)

如果我是你,我会计算帧之间的毫秒数(1000 / fps)并决定每隔一定数量的帧调用一个函数。这个数量超过1:

var counter:int = 0;
const COUNT_TO_INVOKE: int = 2;//if calling every 3 frames
function onEnterFrame(e: Event):void{
    counter++;
    if(counter == COUNT_TO_INVOKE){
        timerFunction();
        counter = 0; 
    }
}

答案 3 :(得分:0)