How to manage code for level screen?

Hi…
I’ve some problem about “How to manage code for level screen”
As code below will be support for first level.
If I’ve 30 levels and have 1 .lua for MainGame
(director:changeScene(“maingame”))
Who’ll giude me to do best solution for level manage,I don’t need to copy code 30 set and don’t have a lot of .lua files for each level.

Thanks in advance

[code]
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

levelmenuText = display.newText(“level menu”, 50,50,system.defaultFont, 50)
levelmenuText.x = 500
levelmenuText.y = 500
localGroup:insert(levelmenuText)

----FUNCTIONS

function myLevel()

director:changeScene(“myLevel”)
end

----ADD EVENT LISTENERS
Runtime:addEventListener(“touch”, myLevel)
return localGroup
end

[/code] [import]uid: 58733 topic_id: 12833 reply_id: 312833[/import]

What i do is create one level.lua (i’m using Director class 1.3) which has all the functionalty for every level.

When i want to load level 1, i use:

local test = {}  
test["level"] = "1" --- \<\< This is variable  
  
director:changeScene( test , "level", "crossFade" )  

In the beginning of level.lua i detect the level parameter :

new = function ( params )  
  
------------------  
-- Params  
------------------  
  
--  
if type( params ) == "table" then  
 --  
 if type( params.level ) == "string" then  
 Level = params.level   
 end  
end  
  

And finally, when loading a level, in this case a level created by levelhelper:

loader = LevelHelperLoader:initWithContentOfFile("level" .. Level .. ".plhs")  

I hope that helps :slight_smile:

[import]uid: 50459 topic_id: 12833 reply_id: 47087[/import]

I think that the easiest way is that you have one lua file that have all the game logic in it.

Then you have one lua file for every level which require the lua file with all the game logic.

I think it all depends on what sort of game you make though. [import]uid: 24111 topic_id: 12833 reply_id: 47092[/import]

Thank rmbsoft, and I’ve question for multiple stage.
If I’ve 30 level that mean I must insert code for 30 buttons

Level_1_Btn - Level_30_Btn
and have 30 functionals for even
level_1_even() - level_30_even()

Do you have any solution for reduce code for this?

[code]
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local gameGroup = display.newGroup()

----FUNCTIONS

function level_1_even()

local test = {}
test[“level”] = “1” — << This is variable

director:changeScene( test , “level”, “crossFade” )
end
Level_1_Btn= ui.newButton{
defaultSrc = “helpbtn.png”,
defaultX = 106,
defaultY = 36,
overSrc = “helpbtn-over.png”,
overX = 106,
overY = 36,
onEvent = level_1_even,
id = “level1”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

Level_1_Btn:setReferencePoint( display.BottomRightReferencePoint )
Level_1_Btn.xOrigin = 480; Level_1_Btn.yOrigin = 450
Level_1_Btn.isVisible = false

gameGroup:insert( Level_1_Btn)
end
[/code] [import]uid: 58733 topic_id: 12833 reply_id: 47129[/import]

for the levels page, i don’t use ui.button, just one touch event:

 local buttons = {}  
 xPos = 0  
 yPos = 200  
  
 for i = 1, 24 do  
  
 if xPos == 900 then  
 xPos = 0  
 yPos = yPos + 150  
 end   
 xPos = xPos + 150  
 buttons[i] = "button"..i  
 if (i-2) \< tonumber(maxLevel) then  
 buttons[i] = display.newImage("level"..i..".png")  
 buttons[i].Level = tostring(i)  
 buttons[i].Name = "button"..i  
 buttons[i]:addEventListener("touch", levelmenu )  
 else  
 buttons[i] = display.newImage("levellocked.png")  
 end  
  
 buttons[i].x = xPos  
 buttons[i].y = yPos  
  
 end  

What the above code does:

It loops through 24 buttons, 4 rows of 6 buttons.
Maxlevel is the highest unlocked level. So if curr number is lower then maxLevel you display that levelx.png, otherwise you display the levellocked image.

benefit of this method is you only use one event to handle all the buttons. You just create a level property for each button (buttons[i].Level) which you use to indentify the levelnumber in the touch event:

 function levelmenu(event)   
 if event.phase == "ended" then  
 local test = {}  
 test["level"] = event.target.Level  
  
 CleanUp()  
 cleanGroup( localGroup )  
 localGroup = nil  
 director:changeScene( test , "level", "crossFade" )  
 end   
 return true   
 end   

[import]uid: 50459 topic_id: 12833 reply_id: 47144[/import]

There are a lot of ways of doing this. If you look at a game like Angry Birds, they give you a screen with several boxes of levels, and each box has a button for each sub-level. But more importantly, beat a level and move on to the next.

You can have a zillion lua files for each level and have that lua file to a require to bring in the game logic, so you only have one game.lua file.

Or go the other way, have one game.lua file that requires the appropriate .lua file that has the level specifics in it. Using this model, it doesn’t have to be a lua file. You could have your level information in a flat text file, JSON file or some other file that doesn’t need complied, that way, if you want to have a way for your players to get new levels without having to download a new game, they could just fetch a data file and away you go. [import]uid: 19626 topic_id: 12833 reply_id: 47149[/import]

Hi rmbsoft, I’m love your code and i’ll try to do as your suggestion thanks for your code :slight_smile:

Hi robmiracle,i’ve one .lua (maingame for control my game), and i’ve game information each level as text file and use maingame for control this information each level, Thanks for your suggestion :slight_smile: [import]uid: 58733 topic_id: 12833 reply_id: 47170[/import]