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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/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
Timer 如果低于或等于0,如何停止计时器_Timer_Lua_Coronasdk_Countdowntimer - Fatal编程技术网

Timer 如果低于或等于0,如何停止计时器

Timer 如果低于或等于0,如何停止计时器,timer,lua,coronasdk,countdowntimer,Timer,Lua,Coronasdk,Countdowntimer,我使用了GitHub的Rob Miracle的代码。它工作正常,但我的问题是,每当它达到00:00或负值时,它会按照我的指示转到另一个场景,但它仍在更新 local secondsLeft = 2 * 60 -- 2 minutes * 60 seconds local clockText = display.newText("02:00", 280, 1, native.systemFontBold, 25) clockText:setFillColor( 1, 0, 0 ) loca

我使用了GitHub的Rob Miracle的代码。它工作正常,但我的问题是,每当它达到00:00或负值时,它会按照我的指示转到另一个场景,但它仍在更新

local secondsLeft = 2 * 60   -- 2 minutes * 60 seconds

local clockText = display.newText("02:00", 280, 1, native.systemFontBold, 25)
clockText:setFillColor( 1, 0, 0 )

local function updateTime()
    -- decrement the number of seconds
    secondsLeft = secondsLeft - 1

    -- time is tracked in seconds.  We need to convert it to minutes and seconds
    local minutes = math.floor( secondsLeft / 60 )
    local seconds = secondsLeft % 60

    -- make it a string using string format.  
    local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
    clockText.text = timeDisplay
end

-- run them timer
local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

听起来你需要调用这个函数。尝试向前声明
countDownTimer
,以便可以在
updateTime()中使用它:

本地倒计时
...
局部函数updateTime()
...

如果时间显示仍在更新的内容?您的问题不清楚。我添加了以下代码:如果时间显示,我将检查您的代码。它很好用。文本对象在到达“00:00”时间后不会更新。先生@David Bowling,我在secondsLeft=secondsLeft-1之前添加了此代码“print(secondsLeft)”。当我转到另一个场景时,它在电晕模拟器的输出中仍然在递减。这就是为什么当它达到1时我会出错的原因:(我很困惑。昨天你说添加
timer.cancel()
有效。现在你是说通过在函数
updateTime()
中添加
print(secondsLeft)
之前添加
secondsLeft=secondsLeft-1
,这就不再有效了吗?顺便说一句,放回勾(`)关于内联代码的可读性。哦,对不起,我忘了在倒计时中删除“local”,它起作用了,先生!:)谢谢:)
local countDownTimer

...

local function updateTime()
...

   if timeDisplay <= "00:00" then
      timer.cancel(countDownTimer)
      print "Game Over"
      GameOver()
   end

...
end

countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )