2017-02-07 10 views
0

Ich habe diesen Code von Rob Miracle von GitHub verwendet. Es funktioniert gut, aber mein Problem ist, dass, wenn es 00:00 oder negativ erreicht, es meiner Anweisung folgt, zu einer anderen Szene zu gehen, aber es wird immer noch aktualisiert.Wie Stoppuhr wenn unter oder gleich 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

Was noch aktualisiert? Deine Frage ist unklar. –

+0

Ich habe diesen Code: if timedisplay <= "00:00" dann \t \t \t print "Game Over" \t \t \t Gameover() \t \t Ende habe ich versucht, die Zeit zu verfolgen, aber wen geht es um die Funktion Spiel vorbei, es wird immer noch dekrementiert. – int21h

+0

Ich überprüfe deinen Code. Es funktioniert gut. Das Textobjekt wird nicht aktualisiert, nachdem die Zeit "00:00" erreicht wurde. – ldurniat

Antwort

0

Es klingt wie Sie die timer.cancel() Funktion aufrufen müssen. Versuchen voraus erklärt countDownTimer, so dass Sie es in updateTime() verwenden können:

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

passiert nichts. immer noch dekrementierend :( – int21h

+1

oh sorry, ich habe vergessen, das "lokale" in countdowntimer zu löschen, es hat funktioniert Herr! :) Danke :) – int21h