timer.cancel

Sorry for posting so many threads here, I’m just getting used to this language

This is a small snippet of my code, I’m trying to cancel the timer from running after the destroy function is called, any help?

function destroy(object) timer.cancel(timeFunction) end function onCollision(event) if (event.phase == "began") then destroy() end end local timeFunction = timer.performWithDelay(1000,startCount,0)

All help is appreciated, thanks!

This is a scope problem. Your function “destroy” doesn’t know what “timeFunction” is because that variable is declared after the destroy function is created. If you adjust the code like this:

local timeFunction function destroy(object) timer.cancel(timeFunction) end function onCollision(event) if (event.phase == "began") then destroy() end end timeFunction = timer.performWithDelay(1000,startCount,0)

…then you should have more luck. It’s good that you are making an effort to use local variables, but it’s important to fully understand scope in Lua to make sure you don’t run into these sorts of problems. Here’s a link to a good primer on scope in Lua & Corona that Rob Miracle wrote earlier this year: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

In your snippet the timeFunction variable is declared as local *after* the declaration of the destroy function.
Try declaring the timeFunction variable before (above) the declaration of the destroy function.
Then you can assign the timer to the variable ad you are doing, but without re-declaring it as local.

Hello! So i wanted to point out a couple things here as i see your new to coding…

Here’s the rewritten code

local timeFunction local function destroy() timer.cancel(timeFunction) end local function onCollision(event) if event.phase == "began" then destroy() end end timeFunction = timer.performWithDelay( 1000, startCount, 0 )

So first off always if you are adding a timer then add a variable at the top of your code.

Next DONT use global function unless nessisary 

EX.

function helloBob() -- global function local function helloBob() -- local function 

And i dont see your “startCount” function that is being called in the timer.

Good Luck!

This is a scope problem. Your function “destroy” doesn’t know what “timeFunction” is because that variable is declared after the destroy function is created. If you adjust the code like this:

local timeFunction function destroy(object) timer.cancel(timeFunction) end function onCollision(event) if (event.phase == "began") then destroy() end end timeFunction = timer.performWithDelay(1000,startCount,0)

…then you should have more luck. It’s good that you are making an effort to use local variables, but it’s important to fully understand scope in Lua to make sure you don’t run into these sorts of problems. Here’s a link to a good primer on scope in Lua & Corona that Rob Miracle wrote earlier this year: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

In your snippet the timeFunction variable is declared as local *after* the declaration of the destroy function.
Try declaring the timeFunction variable before (above) the declaration of the destroy function.
Then you can assign the timer to the variable ad you are doing, but without re-declaring it as local.

Hello! So i wanted to point out a couple things here as i see your new to coding…

Here’s the rewritten code

local timeFunction local function destroy() timer.cancel(timeFunction) end local function onCollision(event) if event.phase == "began" then destroy() end end timeFunction = timer.performWithDelay( 1000, startCount, 0 )

So first off always if you are adding a timer then add a variable at the top of your code.

Next DONT use global function unless nessisary 

EX.

function helloBob() -- global function local function helloBob() -- local function 

And i dont see your “startCount” function that is being called in the timer.

Good Luck!