Hi, Ive been trying to modularize my code, which utilizes director class. I have 50 lvls in my game, and for the most part, only a few variables change from level to level, and I figured it would be much better to have just 1 lua file with the majority of the code in it. I cant figure out how to do it though with director.
I have 1 file called modularizedLevelCode, and then the other 50 level files. Ive tried a few different things… ex: In the modularizedLevel file, I have
module(..., package.seeall)
function new()
local gameGroup = display.newGroup
"code............."
return gameGroup
end
and then in the lvl file…
module(..., package.seeall)
require"modularizedLevelCode"
modularizedLevelCode.new()
end
which doesnt work… I get errors for the director class file. Line 118 and 137 I believe… anyway, Ive tried it several different ways, and no luck… so if anyone knows how to do it, Id appreciate some direction.
[import]uid: 28912 topic_id: 11264 reply_id: 311264[/import]
Ive written a template for how to modularize youre code, while using director class. If your game uses one general level template,
and just changes a few variables from level to level, you can use this template if youre using director class for scene management.
First you have your general level module… lvlModule.lua
Next you need a lvl1.lua file
In the lvlModule put this code.
module(..., package.seeall)
function nextScene(theModule)
TextCandy.CleanUp() -- CLEAN UP \*BEFORE\* SWITCHING SCENES! -- include this if youre using text candy
for i = gameGroup.numChildren,1,-1 do -- this is my main group, where most of my game objects go, I use this statement to clean them out
local child = gameGroup[i] -- when nextScene() is called
child.parent:remove( child )
child = nil
end
for i = popUpGroup.numChildren,1,-1 do -- this is an auxillary group if you need more than one, clean it out the same as above.
local child = popUpGroup[i]
child.parent:remove( child )
child = nil
end
local module = theModule
timer.performWithDelay(250, function()director:changeScene( module, "crossfade" ) end, 1)
end
function callGame()
gameGroup = display.newGroup() --notice the groups are global
popUpGroup = display.newGroup() -- notice the groups are global
---- This is where all of your code goes---------
return gameGroup
end
And in your lvl1 file put this----------------------------------------------------------------------
[code]
module(…, package.seeall)
require"lvlModule"
function new()
lvlModule.callGame() --you can call optional parameters in this function, which will give you access to them in the lvlModule.
gameGroup = display.newGroup()
unloadMe = function()
end
return gameGroup
end
[/code] [import]uid: 28912 topic_id: 11264 reply_id: 40886[/import]