Flash 闪烁AS3定时器极端关闭
我正在使用Flash CS4和AS3。 我想要一个计时器以50毫秒的间隔调用函数100次。然而,计时器所用的时间比它应该用的时间要长得多,重复100次后,总计1677毫秒(1.677秒!)太多了。我是不是漏掉了什么东西,或者计时器有那么不准确 代码Flash 闪烁AS3定时器极端关闭,flash,actionscript-3,timer,Flash,Actionscript 3,Timer,我正在使用Flash CS4和AS3。 我想要一个计时器以50毫秒的间隔调用函数100次。然而,计时器所用的时间比它应该用的时间要长得多,重复100次后,总计1677毫秒(1.677秒!)太多了。我是不是漏掉了什么东西,或者计时器有那么不准确 代码 function test(event:TimerEvent):void{ trace("GetTimer(): " + getTimer() + " || Timer.currentCount: " + _timer.currentCount
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帧速率,其他进程,基本上是电影的一般环境 不要在如此短的时间间隔内使用
计时器。Flash中的计时不是一个简单的主题,请参见开始。要测量50毫秒,我建议使用getTimer()
函数并输入_frameevent来检查时间间隔是否已过。如果我是你,我会计算帧之间的毫秒数(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;
}
}
试着使用自动修正计时器,比如这样
实际FPS可能会因负载而异。是的,有时最好跳过一个函数调用,然后继续加载系统(并扩大帧延迟)。应使用getTimer()。非常重要的是精度。@Muhammad Irfan-getTimer()并不比计时器对象更精确。@Allan-getTimer允许手动回调调用,正如问题所示,计时器可能会累积错误。是的,我知道(我同意你的答案),但这条评论似乎暗示getTimer()是基于一个不同且更精确的计时器系统(我就是这样理解的)。