所以它看起来像你实际上是从“现在”开始几秒钟检查2秒,持续3秒。您可以为enterFrame
事件安排注册和取消注册。使用这将调用您的FuelManage功能中的关注期间每个时间步长:
function cancelCheckFuel(event)
Runtime:removeListener('enterFrame', FuelManager)
end
function FuelManage(event)
if lives > 0 and pressed==true then
lifeBar[lives].isVisible=false
lives = lives - 1
-- print(lifeBar[lives].x)
livesValue.text = string.format("%d", lives)
end
end
-- fuel management:
local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds
local fuelCheckDurationMS = 3000 -- check for 3 seconds
local stopFuelCheckMS = startFuelCheckMS + fuelCheckDurationMS
timer.performWithDelay(
startFuelCheckMS,
function() Runtime:addEventListener('enterFrame', FuelManager) end,
1)
timer.performWithDelay(
stopFuelCheckMS,
function() Runtime:removeEventListener('enterFrame', FuelManager) end,
1)
如果是这种频率太高,那么你要使用一个计时器,并跟踪时间:
local fuelCheckDurationMS = 3000 -- check for 3 seconds
local timeBetweenChecksMS = 200 -- check every 200 ms
local totalCheckTimeMS = 0
local startedChecking = false
function FuelManage(event)
if lives > 0 and pressed==true then
lifeBar[lives].isVisible=false
lives = lives - 1
-- print(lifeBar[lives].x)
livesValue.text = string.format("%d", lives)
end
if totalCheckTimeMS < 3000 then
timer.performWithDelay(timeBetweenChecksMS, FuelManage, 1)
if startedChecking then
totalCheckTimeMS = totalCheckTimeMS + timeBetweenChecksMS
end
startedChecking = true
end
end
-- fuel management:
local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds
timer.performWithDelay(startFuelCheckMS, FuelManage, 1)
每3秒钟3秒?这是一个呼叫从通话时间点延迟2秒? 'timer.performwithDelay(2000,callback,1)'会为你做。 –
我很乐意提供帮助,但“3秒钟内每2秒钟”没有任何意义。第一次调用将在'performWithDelay'之后两秒钟,下一次调用将在此之后的另一个2秒钟内完成,因此从第一次调用到执行延迟时间总共需要4秒,并且不会在“3秒钟”内出现。请澄清。 – Schollii