Search code examples
timerluacoronasdkcorona-storyboard

CoronaSDK - Remove timer from scene on click


I have a newCircle that when clicked changes the color of the circle and starts a timer it it. i want to be able to click the circle again and change it back to black and completely remove the timer from the scene.

delta = 0

local function tapListener( event )
    if (delta == 0) then
        c1:setFillColor(1,1,0)

        local timeLimit = 20
        timeLeft = display.newText(timeLimit, c1.x, c1.y, native.systemFontBold, 14)
        timeLeft:setTextColor(255,0,0)

        local function timerDown()
           timeLimit = timeLimit-1
           timeLeft.text = timeLimit
             if(timeLimit==0)then
                print("Time Out") -- or do your code for time out
             end
          end
        aTimer = timer.performWithDelay(1000,timerDown,timeLimit)            

        delta = delta + 1

    else 
        c1:setFillColor(0,0,0)

        delta = delta - 1


    end

Solution

  • answer:

    else 
        c1:setFillColor(0,0,0)
        timer.cancel( aTimer )
        timeLeft.alpha = 0
        delta = delta - 1
    
    
    end
    

    the timer.cancal( aTimer ) stops the timer and the timeLeft.aplha = 0 hides the text that is displaying the timer