While it’s good that v1.2 shows one way of cleaning up the previous screen, I actually prefer a different method.
In your method, you are calling a “clean()” routine within the screen module. This cannot be a local function. I really hate non-local stuff. What I recommend instead is using a proper cleanup function within the actual screen object itself. Here is an example:
[lua]–Screen2.lua
function new()
local group = display.newGroup()
local back = display.newRect(0,0,display.contentWidth,display.contentHeight)
back:setFillColor(0,0,0)
local t = display.newText(“This is screen 2”, 0, 0)
t:setTextColor(255,255,255)
group:insert(t)
local function touchHandler( event)
if event.phase == “ended” then
director:changeScene(“mainForm”,“moveFromRight”)
end
return true
end
back:addEventListener(“touch”, touchHandler)
local function group:clean()
– do cleanup here
end
return group
end[/lua]
and then in the Director class, instead of your existing callClean routine where you pass the name of the module, pass the current screen instead:
callClean( currScreen)
and then have the code be this:
[lua]local function callClean ( screen )
if screen and screen.clean then screen:clean() end
end[/lua]
Basically it just checks to see if a “clean” routine exists within the screen and if so, it calls it. This allows the clean() to be local to the display group instead of global and also means that the stuff being cleaned up can be local to the group instead of within the module.
Just makes for cleaner code (in my opinion) with less global stuff and better scoping. Not to mention a simpler way to call it from Director instead of looping through the package.loaded table. [import]uid: 12529 topic_id: 6735 reply_id: 306735[/import]