如果低于或等于 0 电晕 sdk 如何停止计时器

how to stop timer if below or equal to 0 corona sdk

我使用了来自 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 )

听起来您需要调用 timer.cancel() 函数。尝试 forward-declaring countDownTimer 以便您可以在 updateTime():

中使用它
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 )