当timer.preformWithDelay 的时间延迟小于帧之间的时间时,计时器将等待进入下一帧以调用该函数。
这意味着,如果您的游戏以 30 或 60 fps 运行,您的“帧毫秒”大约为 16 或 33 毫秒。因此,您可以设置的最小延迟是帧之间的延迟。
在您的情况下,您希望每 1/100 秒或 10 毫秒设置一次计时器。这意味着,由于您的帧很可能是 16 毫秒 (60fps),因此每记录 10 毫秒,您实际上正在等待额外的 6 毫秒。
现在,如果您以 100 FPS 运行并因此达到所说的 10 毫秒,您可以解决此问题,但不推荐这样做。
AlanPlantPot 在coronaLabs 上提供了以下解决方案的答案:
我会改用 enterFrame 函数。您的计时器不会在单毫秒内上升(无论每帧经过多少毫秒,它都会增加),但无论如何没人能读得这么快。
local prevFrameTime, currentFrameTime --both nil
local deltaFrameTime = 0
local totalTime = 0
local txt_counter = display.newText( totalTime, 0, 0, native.systemFont, 50 )
txt_counter.x = 150
txt_counter.y = 288
txt_counter:setTextColor( 255, 255, 255 )
group:insert( txt_counter )
和
local function enterFrame(e)
local currentFrameTime = system.getTimer()
--if this is still nil, then it is the first frame
--so no need to perform calculation
if prevFrameTime then
--calculate how many milliseconds since last frame
deltaFrameTime = currentFrameTime - prevFrameTime
end
prevFrameTime = currentFrameTime
--this is the total time in milliseconds
totalTime = totalTime + deltaFrameTime
--multiply by 0.001 to get time in seconds
txt_counter.text = totalTime * 0.001
end