We'll need to create a couple of functions that activate the countdown and also stop at 0 seconds when the game is over:
Set up the timer countdown for the game with a local function called
myTimer()
:local myTimer = function()
Increment the seconds for the timer countdown by 1. With the
counter
text object, display the time usingnumSeconds
. Print outnumSeconds
to see the countdown in the terminal window:numSeconds = numSeconds - 1 counter.text = "Time: " .. tostring( numSeconds ) print(numSeconds)
Create an
if
statement for when the timer runs out or if all the stars are gone. Within the block, cancel the timer and callcallGameOver()
to end the round. Close themyTimer()
function withend
:if numSeconds < 1 or stars.numChildren <= 0 then timer.cancel(timerInfo) panda:pause() restartTimer = timer.performWithDelay( 300, function() callGameOver(); end, 1 ) end end
Initiate the
myTimer()
function with a new local function called...