System.getTimer

I know, I’m an absolute beginner. But I work hard. One of my goal is now to help somebody on this forum before the end of the year…
At the moment, I need help to understand what I missed and correct my code (or bug?).

I try to mix two examples to practice: gear animation and director.lua (screen1 with animated gear and a button to go to screen2 with flipside mode, and back. Very simple…

The problem appears when I press the button to go to screen2. (“system.getTimer()” seems to be the cause; with timeTable = os.date(“*t”) everything is ok).

–[[Message error
Runtime error
…screen1.lua 41 :attempt to perform aritmetic on field ‘rotation’ (a nil value)
Stsck trsceback
[C]:?
…screen1.lua: 41 in fonction ‘_listener’
?: in function <?:413>
?: in function <?: 186>
]]

–line 41
gear.rotation = gear.rotation – rotationAngle

Why and how to correct (I need system.getTimer)? Thanks in advance for your help.
[import]uid: 8970 topic_id: 3010 reply_id: 303010[/import]

@f2cx

I can’t access the code you work on right now, but have these things in mind:

When you change a scene with Director, all groups of the old scene get removed and the scene-group is deleted. So, if you want to keep some data from scene to scene don’t put it inside a scene but rather in a external module (.lua file) which will play the role of a central data repository (in MVC design pattern it’s called the “Model”, in some programming languages it’s a “Singleton”).

In contrast, the Director class will not remove any listeners or timers! You have to embed a cleanup() function in your new() function which will be called from Director (modify it) or from your “change scene” onTapButton handler.

From your output, I suspect that gear has been nullified by Director, but a listener (which has not been auto-removed keeps calling gear). So, the solution is either to manually remove the listener when button is pressed, or keep this listener if it is used in all scenes but place it (and whatever else it calls) in a separate module which is not controlled by Director.
[import]uid: 7356 topic_id: 3010 reply_id: 8944[/import]

@magenda
Very grateful for your help and detailed explanation ! Now it’s ok.

@beginners:
I remove “timer1” when button to change screen is pressed.
local button02Release = function (event)
timer.cancel (timer1) --to remove timer1
director:changeScene("screen2”, “flip”)
end

–with
timer1 = timer.performWithDelay (500, animate, 0)

@ansca
[import]uid: 8970 topic_id: 3010 reply_id: 8956[/import]