timer.performWithDelay( 1000, function() myCarReadyToGo(mycar) end )

hi Corona fans,

i’m doing a game with cars popping out of the edge of the screen (not unlike ‘airport’ but with cars)

before popping out i’d like to give a warning to the player so i’ve put a timer for 1 second where the I set the car red.

when the timer finishes it goes back to its normal color and goes.

like this 

mycar:setFillColor(255,0,0) -- make the carRED tmmyCarReadyToGo = timer.performWithDelay( 1000, function() myCarReadyToGo(mycar) end ) function myCarReadyToGo(obj) print ("myCarReadyToGo. OK now\>",obj.type) obj.intransit = true obj:setFillColor(1) --\<\<\<\<===== this is where it crash !! end -- myCarReadyToGo ----------------------------------------------------------------------

Now I have many cars hence many timers calling the same function. at different times

this works well enough but if I want to refresh the screen (calling a reset composer scene) while the timer is on , it crashes with

ERROR: Runtime error

21:24:03.694  scene1.lua:1371: attempt to call method ‘setFillColor’ (a nil value)

21:24:03.694  stack traceback:

21:24:03.694  scene1.lua:1371: in function ‘myCarReadyToGo’

21:24:03.694  scene1.lua:189: in function ‘_listener’

21:24:03.694  ?: in function ‘?’

21:24:03.694  ?: in function <?:190>

of course before calling the reset composer screen i try to cancel any running timers. 

but i think that it where the problem lies, that it gets confused about what timer is on ?

am I supposed to create an array of timers for each of the car ? calling the same function ?

help !

ps: it doesnt crash everytime telling me it’s a timing issue. i could probably have the reset button wait longer that the timers themselves (e.g. 2sec) but that would be quite annoying for the player.

Hi @edualc,

You need remove all timers before you remove/go to another  scene otherwise they are still running.

Try (not tested)

mycar.myTimer = timer.performWithDelay( 1000, function() myCarReadyToGo( mycar ) end ) function mycar:finalize( event ) &nbsp; &nbsp; timer.cancel( self.myTimer ) end mycar:addEventListener( "finalize" )

Read more :

Have a nice day:)

ldurniat

This is not a good use of timers.  Much better to have an enterFrame() and use that to spawn objects.  Simply stop and start the enterFrame() when loading/unloading scenes.

When you have this approach, adding a “pause game” is as simple as stopping the enterFrame().

Hi ldurniat,

no that was not the answer, the reason why i put this 

print ("myCarReadyToGo. OK now\>",obj.type)

was to prove that the object still existed (as in obj.type = “CAR”)

The problem was that I was cancelling the timers in my tapRefreshButton function instead of in the  scene:hide function.

(I assume that by the time it reached scene:hide the enterframe has created another timer.)

Now to be honest i never heard of “finalize” and will keep it in mind.

but thank you for your help !

Hi SGS

my objects ARE spawn from the enterframe procedure ! it was stopped by the scene:hide but not the timers so these would still be running.

I like your comment of “When you have this approach, adding a “pause game” is as simple as stopping the enterFrame().” and will use it !

thanks for your help !

Hi @edualc,

You need remove all timers before you remove/go to another  scene otherwise they are still running.

Try (not tested)

mycar.myTimer = timer.performWithDelay( 1000, function() myCarReadyToGo( mycar ) end ) function mycar:finalize( event ) &nbsp; &nbsp; timer.cancel( self.myTimer ) end mycar:addEventListener( "finalize" )

Read more :

Have a nice day:)

ldurniat

This is not a good use of timers.  Much better to have an enterFrame() and use that to spawn objects.  Simply stop and start the enterFrame() when loading/unloading scenes.

When you have this approach, adding a “pause game” is as simple as stopping the enterFrame().

Hi ldurniat,

no that was not the answer, the reason why i put this 

print ("myCarReadyToGo. OK now\>",obj.type)

was to prove that the object still existed (as in obj.type = “CAR”)

The problem was that I was cancelling the timers in my tapRefreshButton function instead of in the  scene:hide function.

(I assume that by the time it reached scene:hide the enterframe has created another timer.)

Now to be honest i never heard of “finalize” and will keep it in mind.

but thank you for your help !

Hi SGS

my objects ARE spawn from the enterframe procedure ! it was stopped by the scene:hide but not the timers so these would still be running.

I like your comment of “When you have this approach, adding a “pause game” is as simple as stopping the enterFrame().” and will use it !

thanks for your help !