Hello,
I have a project which uses Timer and SceneManager. I have come across a weird behaviour in Timer when changing scene. Here is my code:
Scene1 = gideros.class(Sprite)
function Scene1:init(data)
--Timer.stopAll()
local tmr = Timer.new(1000, 1)
tmr:addEventListener(Event.TIMER,
function()
print("scene1")
--this is a predefined function in main.lua which calls scenemanager:changeScene(...)
changeScene("scene1")
end)
tmr:start()
self:addEventListener("exitBegin", function()
Timer.stopAll()
end)
end |
It is a scene that calls itself after timer is complete. Now, if I run this code, "scene1" is printed once and the scene is changed. Then, although the new scene is entered and the timer is started (I have checked with an if statement), there is no "scene1" printed. Event.TIMER event handler isn't called. On the other hand, if I comment out the exitBegin handler and uncomment the Timer.stopAll() on the first line instead, everything works well. What may be the problem? (I even tried adding an enterEnd handler, which calls Timer.resumeAll())
Thanks.
Comments
I believe init is only supposed to be called once on initial scene creation, so you should do stuff to initialise your vars etc in enterBegin, so this is expected behaviour.
This is from a doc I downloaded a while ago...
Events supported by Scene manager are:
• enterBegin: Dispatched when your new scene is about to enter the stage. You can initialize your variables here.
• enterEnd: Dispatched after the transition of new scene has ended. Mostly, you add listeners and start your logic here.
• exitBegin: Dispatched when your current scene is about to leave the stage. You can remove listeners and stop timers here.
• exitEnd: Dispatched after your current scene has leaved the stage.
Cheers
evs
Thanks for the reply. I actually put the code under enterEnd handler and it worked. But the thing is that the init function is called at each scene change (that's how you can give a different argument to the init function at each call). I put a print("...") and it works. The eventlisteners are added inside init, anyway The funny thing is that, although everything inside init function works all the time, when I call timer:start(), it actually starts (print(timer:isRunning()) prints true), but there is no timer event going on. But I guess I will follow your advice.
Thanks.