如何在corona sdk中停止计时器,如果计时器的时间小于或等于0?

我在GitHub上使用了这个代码。它很好用,但我的问题是,每当它到达00:00或负数时,它按照我的指示进入另一个场景,但仍然在更新。

local secondsLeft = 2 * 60   -- 2分钟 * 60秒

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

local function updateTime()
    -- 减少秒数
    secondsLeft = secondsLeft - 1

    -- 时间以秒为单位跟踪。我们需要将其转换为分和秒
    local minutes = math.floor( secondsLeft / 60 )
    local seconds = secondsLeft % 60

    -- 使用字符串格式将其变成字符串。
    local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
    clockText.text = timeDisplay
end

-- 运行计时器
local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )
点赞
用户6879826
用户6879826

看起来你需要调用timer.cancel()函数。尝试提前声明countDownTimer以便您可以在updateTime()中使用它:

local countDownTimer

...

local function updateTime()
...

   if timeDisplay <= "00:00" then
      timer.cancel(countDownTimer)
      print "游戏结束"
      GameOver()
   end

...
end

countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )
2017-02-08 14:44:47