Flash AS3定时器非常关闭
我使用带有AS3的Flash CS4。 我想要一个定时器以50毫秒的间隔调用一个函数100次。然而,定时器比它应该花费的时间要长得多,在100次重复之后,这会增加到1677毫秒(1.677秒!)。我在这里错过了什么,或者是不准确的计时器?Flash AS3定时器非常关闭
代码
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
感谢您的帮助。
问候,
克里斯
有很多因素可以影响一个定时器的准确性。 SWF帧率,其他过程,基本上是您电影的一般环境。
请勿使用Timer
这么小的间隔。在Flash中定时并不是一个简单的话题,请参阅this开始。要测量50 ms,我建议使用getTimer()
函数和ENTER_FRAME事件来检查时间间隔是否已经过去。
如果我是你,我会计算帧之间的毫秒数(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;
}
}
getTimer()应使用非常精确是很重要的。 – 2010-10-21 20:13:45
@Muhammad Irfan - getTimer()不比一个Timer对象更精确。 – Allan 2010-10-21 22:51:08
@Allan - getTimer允许手动回调调用,而Timer可以在问题显示时累积错误。 – alxx 2010-10-22 05:40:02