Switch between levels

Most game sample codes, Lime or not, they have a lua file for every level. That seems to be a lot of work and not very efficient in terms of development time.

There gotta be a better way for that, if I use lime and I name all my level maps like this;

level_1.tmx, level_2.tmx etc

So when my player has found the key and walked to the door or if he beat the boss, the next level loads. Basically it check what level number I am on and if I got the key then load the level with the next higher number.

Another thing I wonder about is when you make a meny like Angry Birds (take them as an example since everyone is doing menus like that) How do I assign a number to a button so it corresponds to the level with the same number?

I was looking at the Lime website and there was no sample on how to load levels, is there any new updates to Lime or new tutorials coming?

Btw, “Building a Platform game with Corona and Lime” tutorial is missing the source files or the links are broken.

Thanks
David [import]uid: 34126 topic_id: 16556 reply_id: 316556[/import]

What I usually do is create a LevelManager class which you can use to keep track of what level the player is on, which ones have been completed/unlocked. Which ones are still locked etc and also to load a specific level based on its index.

I have a Json file with all the level indices and filenames as well as anything else I need associated with them that the manager can then load up.

To assign a number to a button you can just do something like this:

  
local onButtonRelease = function( event )  
  
 -- get the button that was pressed  
 local button = event.target  
  
 -- get the level number property  
 local levelNumber = button.levelNumber  
  
 -- now load the level  
end  
  
-- assign the level id property  
button.levelNumber = 5  
  

This code is assuming that “button” is an already created button and that it has “onButtonRelease” attached to it.

The Building a Platform Tutorial was written by a 3rd party and sadly since then his hosting account has expired, I am waiting for him to send me the resources for the tutorial.

There are updates to Lime and new tutorials coming, it just takes time when you are a one man band :slight_smile: [import]uid: 5833 topic_id: 16556 reply_id: 61823[/import]

Assume I use the director class, with director we write:

director:changeScene("the lua file", "effect")  

I’m not at my Corona computer at the moment so I can’t test this so basically if I do it like this, untested though and not finished…

module(..., package.seeall)  
  
function new()  
 local localGroup = display.newGroup()  
  
 local goToLevel;  
 local target;  
  
 local buttons;  
 local buttonImg = "button.png"  
 local levels;  
 local rows = 4;  
 local columns = 6 ;  
 local i;  
 local j;  
 local padding = 4; -- spacing between icons/buttons  
  
 changeLevel = function(event)  
 target = event.target  
  
 director:changeScene(target.scene, "fade");  
 localGroup:removeSelf();  
 localGroup = nil;  
  
 return true;  
 end  
  
 levels = {}  
 for row = 1, rows do  
 for col = 1, columns do  
 buttons = display.newImageRect(buttonImg, 64, 64);  
 buttons.x =   
 buttons.y =   
 end  
  
 end  
  
  
 for i = 1, #levels do  
 levels[i]:addEventListener("touch", changeLevel);  
 end  
  
 return localGroup  
end  

How would I assign a certain .tmx level file to each button?

In this test I add 24 levels on each “world” screen, I think Angrybirds have 24 or maybe they just 18…

Thanks Graham
/David [import]uid: 34126 topic_id: 16556 reply_id: 61827[/import]

In your levels table that you loop through you could give them all a property so for instance:

  
levels[1].scene = "level1.tmx"  
levels[2].scene = "level2.tmx"  
...  
levels[34].scene = "level34.tmx"  
  

And if you did stick to a naming scheme as simple as that you could do this:

[code]

for i = 1, #levels do
levels[i].scene = “level” … i … “.tmx”
end

[/code] [import]uid: 5833 topic_id: 16556 reply_id: 61829[/import]

Thanks Graham,

Can you please show the json concept you mentioned earlier about the LevelManager Class?

In the last few week theres been so much talk about not using modules and what not so I’m a bit confused. What’s the difference between a class and a module, the Director “class” uses module so I don’t really know what is up or down.

thanks again.
D.

[import]uid: 34126 topic_id: 16556 reply_id: 62053[/import]

Here is a very basic version.
class_levelManager.lua

  
LevelManager = {}  
LevelManager\_mt = { \_\_index = LevelManager }  
  
function LevelManager:new( params )  
  
 local self = {}  
  
 setmetatable( self, LevelManager\_mt )  
  
 self.\_levels = {}  
  
 self.\_levels[#self.\_levels + 1] = { index = 1, file = "level1.tmx" }  
 self.\_levels[#self.\_levels + 1] = { index = 2, file = "level2.tmx" }  
 self.\_levels[#self.\_levels + 1] = { index = 3, file = "level3.tmx" }  
  
 self.\_currentLevelIndex = 1  
  
 return self  
  
end  
  
function LevelManager:setCurrentLevel( index )  
 self.\_currentLevelIndex = index  
end  
  
function LevelManager:getCurrentLevel()  
 return self.\_levels[self.\_currentLevelIndex]  
end  
  

main.lua

  
require( "class\_levelManager" )  
  
local levelManager = LevelManager:new()  
  
-- you can then do things like this  
  
local level = levelManager:getCurrentLevel()  
  
lime.loadMap( level.file )  
  
-- then at the start of the next level you could do this  
  
levelManager:setCurrentLevel( 2 )  
  
local level = levelManager:getCurrentLevel()  
  
lime.loadMap( level.file )  
  

You could also hook this into a button event like so:

  
local onButtonRelease = function( event )  
  
 local button = event.target  
  
 levelManager:setCurrentLevel( button.levelID )  
  
 local level = levelManager:getCurrentLevel()  
  
 lime.loadMap( level.file )  
  
end  
  
local button1 = ui.newButton  
{  
 default = "button-default.png",  
 over = "button-over.png",  
 onRelease = onButtonRelease,  
 text = "Level 1"  
}  
button1.levelID = 1  
  
local button2 = ui.newButton  
{  
 default = "button-default.png",  
 over = "button-over.png",  
 onRelease = onButtonRelease,  
 text = "Level 2"  
}  
button2.levelID = 2  
  
local button3 = ui.newButton  
{  
 default = "button-default.png",  
 over = "button-over.png",  
 onRelease = onButtonRelease,  
 text = "Level 3"  
}  
button3.levelID = 3  
  

Naturally there is alot more you can do with this sort of thing, but hopefully you get the idea. [import]uid: 5833 topic_id: 16556 reply_id: 62092[/import]

If I have 100’s of levels then could I do it like this to save me some typing or am I venturing out in unknow territory?

[code]
function LevelManager:new( params )

local self = {}

setmetatable( self, LevelManager_mt )

self._levels = {}

– Save me some typing…
for i = 1, #self._levels do
self._levels[i + 1] = { file = “level” … i … “.tmx”}
end

–self._levels[#self._levels + 1] = { index = 1, file = “level1.tmx” }
– self._levels[#self._levels + 1] = { index = 2, file = “level2.tmx” }
– self._levels[#self._levels + 1] = { index = 3, file = “level3.tmx” }

self._currentLevelIndex = 1

return self

end
[/code] [import]uid: 34126 topic_id: 16556 reply_id: 62160[/import]

You could do that indeed, however if all your levels are going to use the same naming scheme you could just use this:

  
function LevelManager:getCurrentLevel()  
 return "level" .. self.\_currentLevelIndex .. ".tmx"  
end  
  

However the option you have will allow you to store other data in the level table such as level names, difficulty settings, time durations or anything else you want. [import]uid: 5833 topic_id: 16556 reply_id: 62173[/import]

How do you suggest I should name my levels?

How would you do it?

So If we take Angrybirds level screens, there you have a few “Worlds” that have “levels”. So if I would make it like theirs I could possibly name it like this;

“level_1_1.tmx”

that would translate to;

“level_levelNumber_worldNumber.tmx”

I don’t know if I’ll make the Angrybirds style menu, everyone is doing that so I might do it with a tableView?

Thanks for all the help
David
[import]uid: 34126 topic_id: 16556 reply_id: 62188[/import]

That naming scheme looks good to me. [import]uid: 5833 topic_id: 16556 reply_id: 62195[/import]

Graham,

Going back to post #7

There you suggest this;

LevelManager = {}  
LevelManager\_mt = { \_\_index = LevelManager }  
  
function LevelManager:new(params)  
  
 local self = {}  
  
 setmetatable( self, LevelManager\_mt);  
  
 self.\_levels = {}  
  
 -- Is this code redundant?  
 for i = 1, #self.\_levels do  
 self.\_levels[i + 1] = { file = "level" .. i .. ".tmx"}  
 end  
  
 --self.\_levels[#self.\_levels + 1] = { index = 1, file = "level1.tmx" }  
 --self.\_levels[#self.\_levels + 1] = { index = 2, file = "level2.tmx" }  
 --self.\_levels[#self.\_levels + 1] = { index = 3, file = "level3.tmx" }  
  
 self.\_currentLevelIndex = 1  
  
 return self  
  
end  
  
function LevelManager:setCurrentLevel( index )  
 self.\_currentLevelIndex = index  
end  
  
 --function LevelManager:getCurrentLevel()  
 --return self.\_levels[self.\_currentLevelIndex]  
 --end  
  
function LevelManager:getCurrentLevel()  
 return "level" .. self.\_currentLevelIndex .. ".tmx"  
end  

Or did I misunderstand the concept?

Another thing I read on the Lime website was saving map formats, what are the differences in performance and file size? How would you save the maps?

As for performance and target devices I focus on minimum 3GS but thinking of dropping them too since I don’t have a device to test that on when I come to that stage. So I go for ipads and iphone4/s even though I would miss out on some sales/downloads.

Anyways, Thanks for the help. The money I spent on Lime has already been paid back in 100’s, well worth the money.

Thanks
David [import]uid: 34126 topic_id: 16556 reply_id: 62326[/import]

Nope that was correct, you didn’t misunderstand. Was just suggesting another way :slight_smile:

For map format, in-game performance will be the same with each of them as they are all parsed into memory however for storage space the XML is the worst taking up the most, CSV takes up less and then the compressed formats take up even less. [import]uid: 5833 topic_id: 16556 reply_id: 62334[/import]