Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua CoronasSDK-实现游戏计时器计数毫秒_Lua_Coronasdk - Fatal编程技术网

Lua CoronasSDK-实现游戏计时器计数毫秒

Lua CoronasSDK-实现游戏计时器计数毫秒,lua,coronasdk,Lua,Coronasdk,我使用timer.performWithDelay来计算玩家完成一个关卡所需的时间。我希望它能降到100分之一秒,因为游戏是多人游戏,我不希望有太多的平局 以下是我所做的: local totaltime = 0 local function counter() totaltime = totaltime + 0.01 print(totaltime) end timer1 = timer.performWithDelay( 10, counter, 0) 结果是每秒钟大约

我使用timer.performWithDelay来计算玩家完成一个关卡所需的时间。我希望它能降到100分之一秒,因为游戏是多人游戏,我不希望有太多的平局

以下是我所做的:

local totaltime = 0

local function counter()
    totaltime = totaltime + 0.01
    print(totaltime)
end

timer1 = timer.performWithDelay( 10, counter, 0)
结果是每秒钟大约持续4秒。这是不实用还是有缺陷?

当timer.preformWithDelay被赋予一个小于帧间时间的时间延迟时,计时器将等待下一帧被输入以调用该函数

这意味着如果你有一个以30或60 fps的速度运行的游戏,你的“帧毫秒”大约是16或33毫秒。所以你能设置的最小延迟是帧之间的延迟

在您的情况下,您希望每隔1/100秒或10毫秒设置一次计时器。这意味着,由于您的帧最有可能是16ms 60fps,因此每记录10ms,您实际上会额外等待6ms

现在你可以解决这个问题,如果你以100 FPS的速度运行,从而达到10 ms,但这是不推荐的

AlanPlantPot提供了以下解决方案的答案:

我将使用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