Advertisement
lvs

Director Class in 24 lines

lvs
Oct 29th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. local director, coronaMetaTable = {scene = 'main'}, getmetatable(display.getCurrentStage())
  2. local function cleanGroups (object)
  3.     if type(object) ~= 'table' or getmetatable(object) ~= coronaMetaTable then return end
  4.     if object.numChildren then
  5.         while object.numChildren > 0 do
  6.             cleanGroups(object[object.numChildren])
  7.         end
  8.     end
  9.     object:removeSelf()
  10. end
  11. function director:changeScene (moduleName)
  12.     if type(moduleName) == 'nil' or self.scene == moduleName then return end
  13.     local loadedModule = package.loaded[self.scene]
  14.     if type(loadedModule) == 'table' and loadedModule.clean and type(loadedModule.clean) == 'function' then
  15.         loadedModule.clean()
  16.     end
  17.     cleanGroups(self.view)
  18.     if self.scene ~= 'main' and type(loadedModule) == 'table' then
  19.         package.loaded[self.scene], loadedModule = nil
  20.         collectgarbage('collect')
  21.     end
  22.     self.view, self.scene = require(moduleName).new(), moduleName
  23. end
  24. return director
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement