Hi Guys,
The Director forum may be a better place for this, but
I made a quick little director example to use a table
as in Rob’s example above…works great…
…look no globals…
main.lua
--\*\* main.lua
--\*\* This is your basic main.lua file keep it simple
--\*\* the only thing you really need to add is global vars
--\*\* or table. (and this is how you do it WITH-OUT global vars.)
display.setStatusBar( display.HiddenStatusBar );
--\*\* I used modified director.lua file: ---- modified ver. 1.4 used ----
--\*\* from this post ( link is posted in #6 reply)
--\*\*
--\*\* http://developer.anscamobile.com/forum/2011/09/07/modified-director-jonathan-beebes-blog-post
--\*\* Thanks for all that helped with this ver. of director
--\*\*
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*NOTE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
--\*\*
--\*\* all tho' you CAN use the ORIGINAL Director Class ver. 1.4 the
--\*\* version linked above just gets rid of the "module( ..., package.seeall )"
--\*\* and replaces it with a table. Lua eventually will do away with
--\*\* the "module( ..., package.seeall )" in future versions, as posted
--\*\* on their main lua site. http://www.lua.org
--\*\*
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*END OF NOTE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
director = require("director");
local mainGroup = display.newGroup();
--\*\*
--\*\* make our table that we can pass all through our director class
--\*\* and use the contents with-in our modules to change or use as is.
--\*\*
local myTable = {};
myTable.whiteshoe = 164;
myTable.brownshoe = 18;
myTable.greenshoe = 1507;
myTable.redshoe = 1453;
--\*\* myTable.purpleshoe = true; --\*\*
--\*\* myTable.gameSave = false; --\*\*\* you can add what-ever you like
--\*\* myTable.buttonStatus = true; --\*\*\* all our vars to keep track of
--\*\* myTable.hotgame = 1234569; --\*\*
myTable.mytext = "Game over!";
myTable.gmenu = "Game Menu";
myTable.g1 = "Game Scene 1";
myTable.g2 = "Game Scene 2";
myTable.g3 = "Game Scene 3";
myTable.screenWidth = display.contentWidth; --\*\* our screen width
myTable.screenHeight = display.contentHeight; --\*\* our screen height
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*END OF OUR TABLE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
local main = function()
mainGroup:insert(director.directorView);
director:changeScene(myTable, "gamemenu", "crossfade");
return true;
end
main();
return true;
gamemenu.lua
--\*\* gamemenu.lua
local M = {};
--\*\* local vars here if you need them
--\*\* in the module
M.new = function(params)
local localGroup = display.newGroup();
--\*\*
--\*\* make a local table to hold our passed params
--\*\*
local myTable = {};
if type (params) == "table" then
myTable = params;
end
--\*\*
--\*\* for testing our table inside main.lua
--\*\* prints to simulator output window
print("\nInside gamemenu.lua");
print("greeshoe: ", myTable.greenshoe);
print("whiteshoe: ", myTable.whiteshoe);
print("brownshoe: ", myTable.brownshoe);
--\*\*
--\*\* put more functions / game logic in this area for this scene / module
--\*\*
--\*\* example button only
local playButton\_menu = display.newRect((myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 400, 220, 50);
playButton\_menu.strokeWidth = 3;
playButton\_menu:setFillColor(140, 140, 140);
playButton\_menu:setStrokeColor(180, 180, 180);
playButton\_menu.scene = "gamescene1";
localGroup:insert(playButton\_menu);
--\*\* display our text on to the button
local buttonText = display.newText(myTable.gmenu, (myTable.screenWidth / 2) - 80, (myTable.screenHeight / 2) - 400, native.systemFont, 16\*2);
buttonText:setTextColor(255, 255, 255);
localGroup:insert(buttonText);
--\*\*
--\*\* our move to next scene function
--\*\*
local function nextSceneSelect(e)
if(e.phase == "ended") then
--\*\* clean up our garbage before we move on / director class also cleans it's self
local cleanTmr = timer.performWithDelay(1, function() collectgarbage("collect") end);
timer.cancel(cleanTmr);
director:changeScene(myTable, e.target.scene, "crossfade");
end
return true;
end
--\*\*
--\*\* our button event listener
--\*\*
playButton\_menu:addEventListener("touch", nextSceneSelect);
--\*\*
--\*\* always return our group
--\*\*
return localGroup;
end
--\*\*
--\*\* always have to return
--\*\*
return M;
gamescene1.lua
--\*\* gamescene1.lua
local M = {};
--\*\* local vars here if you need them
--\*\* in the module
M.new = function(params)
local localGroup = display.newGroup();
--\*\*
--\*\* make a local table to hold our passed params
--\*\*
local myTable = {};
if type (params) == "table" then
myTable = params;
end
--\*\*
--\*\* for testing our table inside main.lua
--\*\* prints to simulator output window
print("\nInside gamescene1.lua");
print("greeshoe: ", myTable.greenshoe);
print("whiteshoe: ", myTable.whiteshoe);
print("brownshoe: ", myTable.brownshoe);
--\*\*
--\*\* add two shoes together (each time we are in this module / scene)
--\*\*
myTable.brownshoe = (myTable.brownshoe + myTable.greenshoe);
--\*\*
--\*\* put more functions / game logic in this area for this scene / module
--\*\*
--\*\* example button only
local playButton\_menu = display.newRect((myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 300, 220, 50);
playButton\_menu.strokeWidth = 3;
playButton\_menu:setFillColor(140, 140, 140);
playButton\_menu:setStrokeColor(180, 180, 180);
playButton\_menu.scene = "gamescene2";
localGroup:insert(playButton\_menu);
--\*\* display our text on to the button
local buttonText = display.newText(myTable.g1, (myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 300, native.systemFont, 16\*2);
buttonText:setTextColor(255, 255, 255);
localGroup:insert(buttonText);
--\*\*
--\*\* our move to next scene function
--\*\*
local function nextSceneSelect(e)
if(e.phase == "ended") then
--\*\* clean up our garbage before we move on / director class also cleans it's self
local cleanTmr = timer.performWithDelay(1, function() collectgarbage("collect") end);
timer.cancel(cleanTmr);
director:changeScene(myTable, e.target.scene, "crossfade");
end
return true;
end
--\*\*
--\*\* our button event listener
--\*\*
playButton\_menu:addEventListener("touch", nextSceneSelect);
--\*\*
--\*\* always return our group
--\*\*
return localGroup;
end
--\*\*
--\*\* always have to return
--\*\*
return M;
gamescene2.lua
--\*\* gamescene2.lua
local M = {};
--\*\* local vars here if you need them
--\*\* in the module
M.new = function(params)
local localGroup = display.newGroup();
--\*\*
--\*\* make a local table to hold our passed params
--\*\*
local myTable = {};
if type (params) == "table" then
myTable = params;
end
--\*\*
--\*\* for testing our table inside main.lua
--\*\* prints to simulator output window
print("\nInside gamescene2.lua");
print("greeshoe: ", myTable.greenshoe);
print("whiteshoe: ", myTable.whiteshoe);
print("brownshoe: ", myTable.brownshoe);
--\*\*
--\*\* put more functions / game logic in this area for this scene / module
--\*\*
--\*\* example button only
local playButton\_menu = display.newRect((myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 200, 220, 50);
playButton\_menu.strokeWidth = 3;
playButton\_menu:setFillColor(140, 140, 140);
playButton\_menu:setStrokeColor(180, 180, 180);
playButton\_menu.scene = "gamescene3";
localGroup:insert(playButton\_menu);
--\*\* display our text on to the button
local buttonText = display.newText(myTable.g2, (myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 200, native.systemFont, 16\*2);
buttonText:setTextColor(255, 255, 255);
localGroup:insert(buttonText);
--\*\*
--\*\* our move to next scene function
--\*\*
local function nextSceneSelect(e)
if(e.phase == "ended") then
--\*\* clean up our garbage before we move on / director class also cleans it's self
local cleanTmr = timer.performWithDelay(1, function() collectgarbage("collect") end);
timer.cancel(cleanTmr);
director:changeScene(myTable, e.target.scene, "crossfade");
end
return true;
end
--\*\*
--\*\* our button event listener
--\*\*
playButton\_menu:addEventListener("touch", nextSceneSelect);
--\*\*
--\*\* always return our group
--\*\*
return localGroup;
end
--\*\*
--\*\* always have to return
--\*\*
return M;
gamescene3.lua
--\*\* gamescene3.lua
local M = {};
--\*\* local vars here if you need them
--\*\* in the module
M.new = function(params)
local localGroup = display.newGroup();
--\*\*
--\*\* make a local table to hold our passed params
--\*\*
local myTable = {};
if type (params) == "table" then
myTable = params;
end
--\*\*
--\*\* for testing our table inside main.lua
--\*\* prints to simulator output window
print("\nInside gamescene3.lua");
print("greeshoe: ", myTable.greenshoe);
print("whiteshoe: ", myTable.whiteshoe);
print("brownshoe: ", myTable.brownshoe);
print("Our Text String: ", myTable.mytext);
--\*\*
--\*\* put more functions / game logic in this area for this scene / module
--\*\*
--\*\* example button only
local playButton\_menu = display.newRect((myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 100, 220, 50);
playButton\_menu.strokeWidth = 3;
playButton\_menu:setFillColor(140, 140, 140);
playButton\_menu:setStrokeColor(180, 180, 180);
playButton\_menu.scene = "gamemenu";
localGroup:insert(playButton\_menu);
--\*\* display our text on to the button
local buttonText = display.newText(myTable.g3, (myTable.screenWidth / 2) - 100, (myTable.screenHeight / 2) - 100, native.systemFont, 16\*2);
buttonText:setTextColor(255, 255, 255);
localGroup:insert(buttonText);
--\*\* display our game over text
local gameovertext = display.newText(myTable.mytext, (myTable.screenWidth / 2) - 80,(myTable.screenHeight / 2), native.systemFont, 16\*2);
gameovertext:setTextColor(100, 100, 100);
localGroup:insert(gameovertext);
--\*\*
--\*\* our move to next scene function
--\*\*
local function nextSceneSelect(e)
if(e.phase == "ended") then
--\*\* clean up our garbage before we move on / director class also cleans it's self
local cleanTmr = timer.performWithDelay(1, function() collectgarbage("collect") end);
timer.cancel(cleanTmr);
director:changeScene(myTable, e.target.scene, "crossfade");
end
return true;
end
--\*\*
--\*\* our button event listener
--\*\*
playButton\_menu:addEventListener("touch", nextSceneSelect);
--\*\*
--\*\* always return our group
--\*\*
return localGroup;
end
--\*\*
--\*\* always have to return
--\*\*
return M;
This all should work fine, as I tested and copied here…
Interesting tho, how it all works great…
Tested with Profiler.lua and memory is…after load
and playing…goes flatline…(stable) 
EDIT: cleaned it up a little and added a button/name
for every scene and more vars to our table to help explain
it all a little better / how it all works…
Happy coding 
Larry
Willow Road games [import]uid: 107633 topic_id: 21917 reply_id: 87187[/import]