timer and function

I have this timer

timer.performWithDelay(1, function() if line then line.rotation = line.rotation + 15 end end , 60)  
  
--comments  
--comments  
--comments  
--comments  
--comments  
--comments  
--comments  
  

the problem is that occasionaly the line object gets removed before the function is able to perform 60 times… and I end up getting an error… attempting to compare nil with a number. I tried adding that if then statement in the anonymous function, thinking it might make it a little safer… My question, can anyone make any recommendations as to how to make this timer safe? [import]uid: 28912 topic_id: 12829 reply_id: 312829[/import]

doing something like this

line = display.newLine( 100, 100, 130, 130 )  
timer.performWithDelay(1, function() if line then line.rotation = line.rotation + 15 end end , 60)  

it works like a charm… [import]uid: 70635 topic_id: 12829 reply_id: 47072[/import]

haha, well… yeah that works… its what I had! But, try deleting the line object before all the functions complete… will prolly produce an error… Im trying to figure out how to avoid that error
[import]uid: 28912 topic_id: 12829 reply_id: 47091[/import]

How about doing it this way:

local line = display.newLine( 100, 100, 130, 130 ) local myTimer local myFunction() if line then line.rotation = line.rotation + 15 else if myTimer then timer.cancel(myTimer) end end end myTimer = timer.performWithDelay(1, myFunction, 60) [import]uid: 23636 topic_id: 12829 reply_id: 47098[/import]

It doesnt appear as if any of these solutions work… for instance with this code

local line = display.newLine( 100, 100, 130, 130 )  
local myTimer  
local myFunction()  
 if line then   
 line.rotation = line.rotation + 15  
 else  
 if myTimer then timer.cancel(myTimer) end   
 end  
end  
myTimer = timer.performWithDelay(1, myFunction, 60)  

if you add the following line of code to remove the line before myFunction has fired 60 times… you get an error

timer.performWithDelay(55, function() if line then line:removeSelf() end, 1)  
--comments  
--comments  
--comments  
--comments  
--comments  
--comments  

I thought adding pcall() to the code would help
ex:

local line = display.newLine( 100, 100, 130, 130 )  
  
local myTimer  
local function test(event)  
local t = event.source  
local line = t.line  
line.rotation = line.rotation + 15   
end  
myTimer = timer.performWithDelay(1, pcall(test), 100)  
myTimer.line = line  

doesnt work either though, using pcall to call test seems to cause an error with myTimer.

Anyone more ideas as to how I can make this work?

[import]uid: 28912 topic_id: 12829 reply_id: 47236[/import]

After your added line of code to call line:removeSelf() before myFunction() has fired 60 times, add this addition line:

line = nil [import]uid: 23636 topic_id: 12829 reply_id: 47243[/import]

Oops!
The last response by me was an error and won’t work. Here is the idea done as tested in a complete main.lua file:

print("Start") local line = display.newLine( 100, 100, 130, 130 ) local myTimer local function myFunction() if line then line.rotation = line.rotation + 15 print("rotate") else if myTimer then timer.cancel(myTimer) end print("Done!") end end local function myRemove() line:removeSelf() line = nil end myTimer = timer.performWithDelay(1, myFunction, 60) timer.performWithDelay(55, myRemove, 1) [import]uid: 23636 topic_id: 12829 reply_id: 47289[/import]

sweet, yeah this works well! Wonder why the line has to be nil’d out in order to avoid the runTime error? [import]uid: 28912 topic_id: 12829 reply_id: 47294[/import]

That is a best practices coding for memory management. See for example this: http://jonbeebe.net/corona-sdk-memory-management-101-tag-memory-m

The local variable “line” still has a memory reference to the display object, even though it is not there anymore after the line:removeSelf().

Besides, that is the test for you to use when deciding to cancel the timer. [import]uid: 23636 topic_id: 12829 reply_id: 47314[/import]