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

时间:2017-02-07 15:12:11

标签: timer lua corona 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 )

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 )

1 个答案:

答案 0 :(得分:0)

听起来你需要调用timer.cancel()函数。尝试向前声明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 )