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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

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

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

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

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

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.

Here is a very basic version.

class_levelManager.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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

1
2
3
function LevelManager:getCurrentLevel()
     return "level" .. self._currentLevelIndex .. ".tmx"
end

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

That naming scheme looks good to me.

Graham,

Going back to post #7

There you suggest this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

Nope that was correct, you didn't misunderstand. Was just suggesting another way :-)

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.

views:1957 update:2011/10/22 17:28:16
corona forums © 2003-2011