Scenes are simple, but at the same time they are complex. You have to have a grasp on scope (when variables/functions are visible to other functions), how modules are loaded and unloaded, and how events work.
To sum it up:
Code that’s not inside a function in a scene module gets executed once, when the module is initially required. Your timer and the code to create the display.newText() fall into this category.
scene:create() gets called before scene’s are put on the screen. You put things here needed to create the scene, such as your display.newText().
scene:show() is where you start timers.
scene:hide() is where you stop timers.
scene:destroy() is where you get rid of things like audio that you created in scene:create(). scene:create() and scene:destroy() are basically called in pairs (though unless the scene is never removed). scene:show() and scene:hide() will always be called in pairs.
main.lua is never a scene. But anything needed to initialize the app’s run, is typically done in main.lua.
In your example, I would put your “generate” function near the top, but not inside any other function. Then in scene:show() (it will get called twice, once with a phase of “will” and once with a phase of “did”. When you get the did phase then the scene is on screen, then you call your timer.
Your display.newText() is something that should be in scene:create() but since you need to reference it elsewhere (i.e. other functions) at the top of your scene do:
local text – you probably should give it a more descriptive name
Then inside scene:create do:
text = display.newText(…) – leave off the local here.
Then things will work as you expect.
Rob