我使用timer.performWithDelay来确定玩家需要多长时间才能完成一个等级。我想把它降到100秒(因为游戏是多人游戏,我不希望有太多的平局)。
以下是我所做的:
local totaltime = 0
local function counter()
totaltime = totaltime + 0.01
print(totaltime)
end
timer1 = timer.performWithDelay( 10, counter, 0)它的结果是每秒钟持续约4秒。这是不实际的,还是某处有缺陷?
发布于 2014-06-29 16:57:37
当timer.preformWithDelay被赋予更小的时间延迟时,计时器将等待下一个帧被输入来调用函数。
这意味着,如果你的游戏运行在30或60英尺,你将有一个大约16或33毫秒的“帧ms”。所以,你可以放的最小延迟是帧之间的延迟。
在您的情况下,您希望每1/100秒钟设置一次计时器,或者使用10 of。这意味着,由于您的帧很可能是16 6ms (60 6ms),每记录10 6ms,您实际上是在等待一个额外的6ms。
现在你可以解决这个问题,如果你运行100 FPS,从而达到说10 ms,但这是不值得推荐的。
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
endhttps://stackoverflow.com/questions/24477016
复制相似问题