timer.cancel() returns "Attempt to index a nil value"

Hello guys, I’m trying to cancel a timer started in a “touch event” function inside another “touch event” function, as shown below:  

local function startNewGame(event) if(event.phase=="ended")then local function animationImmaginiOggetti() for i=1, 7 do transition.to( immaginiOggettiAvvioPartita[i], { time=200, delay=0, xScale=0, yScale = 0, alpha = 0} ) end end local function removeImmaginiOggetti() if immaginiOggettiAvvioPartita[1] then for i=1, 11 do immaginiOggettiAvvioPartita[i]:removeSelf() immaginiOggettiAvvioPartita[i] = nil end end end local tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1) local tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1) end end local function replayGame(event) if(event.phase=="ended")then timer.cancel(tmrAIO) timer.cancel(tmrRIO) end end startBTN:addEventListener("touch", startNewGame) replayBTN:addEventListener("touch", replayGame)

My problem is that corona returns “File: ? Attempt to index a nil value” on timer.cancel (tmrAIO). 

What am I doing wrong?

Thank you,

BR.

This means that you are passing nil into the timer.cancel() function.  You need test if variables “tmrAIO” and “tmrRIO” are nil first like this…

if (tmrAIO) then timer.cancel(tmrAIO) tmrAIO = nil end if (tmrRIO) then timer.cancel(tmrRIO) tmrRIO = nil end

The other thing is your variables are local to that ‘if (event.phase==“ended”) then’ block. So when that if/then ends, the local variable references are released.

What you need to do is declare those variables in the scope of your module, like this…

local tmrAIO local tmrRIO local function startNewGame(event) if(event.phase=="ended")then local function animationImmaginiOggetti() for i=1, 7 do transition.to( immaginiOggettiAvvioPartita[i], { time=200, delay=0, xScale=0, yScale = 0, alpha = 0} ) end end local function removeImmaginiOggetti() if immaginiOggettiAvvioPartita[1] then for i=1, 11 do immaginiOggettiAvvioPartita[i]:removeSelf() immaginiOggettiAvvioPartita[i] = nil end end end tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1) tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1) end end

Of course, it’s lua, so there’s a million ways to do it. :slight_smile:

Good catch @no2games.

I didn’t notice those variables were declared local within that function.  :slight_smile:

Thank you both! :smiley: I tried what @no2games said and now it is working!

I would do the nil checks too, BTW…

Yeah after my previous reply I’ve added the checks on the nil :wink:

This means that you are passing nil into the timer.cancel() function.  You need test if variables “tmrAIO” and “tmrRIO” are nil first like this…

if (tmrAIO) then timer.cancel(tmrAIO) tmrAIO = nil end if (tmrRIO) then timer.cancel(tmrRIO) tmrRIO = nil end

The other thing is your variables are local to that ‘if (event.phase==“ended”) then’ block. So when that if/then ends, the local variable references are released.

What you need to do is declare those variables in the scope of your module, like this…

local tmrAIO local tmrRIO local function startNewGame(event) if(event.phase=="ended")then local function animationImmaginiOggetti() for i=1, 7 do transition.to( immaginiOggettiAvvioPartita[i], { time=200, delay=0, xScale=0, yScale = 0, alpha = 0} ) end end local function removeImmaginiOggetti() if immaginiOggettiAvvioPartita[1] then for i=1, 11 do immaginiOggettiAvvioPartita[i]:removeSelf() immaginiOggettiAvvioPartita[i] = nil end end end tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1) tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1) end end

Of course, it’s lua, so there’s a million ways to do it. :slight_smile:

Good catch @no2games.

I didn’t notice those variables were declared local within that function.  :slight_smile:

Thank you both! :smiley: I tried what @no2games said and now it is working!

I would do the nil checks too, BTW…

Yeah after my previous reply I’ve added the checks on the nil :wink: