Below is the structure I use. Declare things you will need throughout the scene at the top of the file. For example you can set up an table for local variables and graphics objects, which can then be accessed or added to later.
[lua]
local v = {}
v.currentScore = 0
v.difficulty = 3
local gfx = {}
gfx.bg = display.newImageRect(“bg.png”, 480, 320) – within create scene
[/lua]
You could then say hide the background in a function later on with gfx.bg.alpha = 0.
SCENE TEMPLATE:
[lua]
– LIBRARIES
local storyboard = require(“storyboard”)
– DISPLAY GROUPS
local group = display.newGroup()
– LOCAL VARIABLES
local scene = storyboard.newScene()
– OBJECT DECLARATIONS
– MY SCENE FUNCTIONS
– STORYBOARD FUNCTIONS
– Called when the scene’s view does not exist:
function scene:createScene(event)
group = self.view
end
– Called BEFORE scene has moved onscreen:
function scene:willEnterScene(event)
group = self.view
end
– Called immediately after scene has moved onscreen:
function scene:enterScene(event)
group = self.view
end
– Called when scene is about to move offscreen:
function scene:exitScene(event)
group = self.view
end
– Called AFTER scene has finished moving offscreen:
function scene:didExitScene(event)
group = self.view
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene(event)
group = self.view
end
– Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan(event)
group = self.view
local overlay_scene = event.sceneName – overlay scene name
end
– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded(event)
group = self.view
local overlay_scene = event.sceneName – overlay scene name
end
– END OF YOUR IMPLEMENTATION
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“willEnterScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“didExitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
scene:addEventListener(“overlayBegan”, scene)
scene:addEventListener(“overlayEnded”, scene)
return scene
[/lua]