2017-02-07 34 views
0

我使用了Rob Miracle從GitHub獲得的代碼。它工作正常,但我的問題是,無論何時到達00:00或負面,它遵循我的指令去另一個場景,但它仍在更新。如何停止計時器,如果低於或等於0 corona sdk

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) 
+0

什麼仍在更新?你的問題不清楚。 –

+0

添加此代碼:if timeDisplay <=「00:00」然後 \t \t \t打印「遊戲結束」 \t \t \t GAMEOVER() \t \t端我試圖跟蹤的時間,但文它轉到功能遊戲結束,它仍然在遞減。 – int21h

+0

我檢查你的代碼。它工作正常。文本對象在達到「00:00」時間後不會更新。 – ldurniat

回答

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) 
+0

什麼也沒有發生。仍然遞減:( – int21h

+1

哦對不起,我忘了刪除countdowntimer中的'本地',它的工作先生!:)謝謝你:) – int21h