what contains level ?

hi,

I have wrote a playable game and now i would like to make different level.

what does contain my main.lua ?

What does contains my level’s files ?

I have searching an example code of a game with different levels but i have found nothing…have you some example to understand ?

Thanks for your help .

anybody ?

just an example of game with levels help me to understand…

There’s a few different ways you could do it, depending on how far you’re going with the game. You only have one main.lua for the entire thing. Beyond that, you could write each level as its own scene (wasteful, imo), or you could define all of your level info in a file or table, and for each new level just read the apporiate entry for that level to get the variable states.

To add to what @akuthia said. many people will use a scene manager like our composer API that will handle the different scenes, levels etc.  In that case main.lua is a place to initialize game wide things (like in app purchases, ad networks, key event handlers for Android) and then it calls your first scene, which could be a splash scene, an intro scene or go straight to your menu.

Then from your menu you might have user scenes (which are .lua files) for help, credits, settings, etc.  As for the game, there are too many variables for how you might want to do this to say this is “how to do it”.  But in the game I’m currently building, I have a “levelselect.lua” that determines what level the player wants to play.  It then loads my “game.lua” scene.  The game.lua scene takes the selected level from the level select screen, then looks up a bunch of  data for that level (speed of play, number and types of enemies, etc) and game.lua plays that level with those parameters.  When the level finishes, it either goes to “gameover.lua” or “nextlevel.lua” depending on the success of the player. 

This model is fine if each level is basically the same.  A game like Angry Birds can be built like this since you can have data define the background images, number of birds, position and types of walls and pigs.   But sometimes, if you wanted to use some scene design tool, then each level might be it’s own scene with it’s unique features.  This can go either way depending on how independent each scene is.  I did a kids game once for a client  and it was over 100 scenes of play.  Some scenes got re-used and I think it ended up being a total of 70 .lua files to build.

Rob

thank you both of you. but is it possible to find an example file with separate levels of main.lua ?

it will be more meaningful to me …

Hi Espace3D,

What type of game are you making? I wasted a couple of months trying to find out how to do things “the right way”, only to realise that there is no right way: everything depends on the project you are working on, and typically I organise things completely differently depending on each project (in type and scale).

Knowing your game better will help find the answer to your questions.

Hi Thomas6,

i make a game with one screen non-movable per level.

i work with a gridgame…

if it help you …

If it’s just a gridgame, I don’t think you necessarily need separate modules - although I do like the use of modules myself to keep the core game itself apart from menu screens etc… Your several levels could be in a simple table, I presume, with each table entry consisting of another table that holds the content for each cell in your grid, if you know what I mean?

Something like:

levelData = { {1,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1}, {2,1,0,0,2,2,0,0,3,1,0,4,1,1,1,1}, }

Where every horizontal line contains the data for one level - and 0 would mean “empty”, 1 would mean “carrot” and 2 would mean “diamond”, for example.

yes

For a question of coherence i prefer to separate my level than my main.lua file.
have you not a short example of this ?

Make a file called “levelData.lua” than contains:

local data = { and here the table containing your leveldata as described above } return data

Then in your “main.lua” or any other module write

local levelData = require("levelData")

and this local value “levelData” will be the table that you put in the separate file.

hi, thomas6

i have tried what you explain.

my table appears but it seems that my table.touch don’t work. 

Is it something special to do with EventListener and a module (mytable) on a separated file ? or it’s a error depending on me ?

I can’t really answer that question because you could be doing a million things wrong. I am puzzled by why you have something like “table.touch”, because a table is not a display object, so you can’t touch a table. Furthermore, I can’t see why you’d want to declare a touch function as a part of a table.

Do you add the touch eventListener to a displayGroup holding your grid elements, to each separate element or just as a runtime eventlistener? For starters, try to put the touch function in it’s own separate variable, and not as part of any table.

Cheers,

Thomas

p.s. As far as I know, the predeclared “table” object in Lua does not have a “touch” property or method.

By the way, I get the feeling that you should be sharing some code and / or screenshots if you really want to get helpful responses.

Thomas is right, you need to post code for any meaningful help.  Select the code in your editor, and do a “Copy” (CTRL-C on Windows, CMD-C on a Mac).  In the Forum reply box type in:

[code]  (without the space)

Paste your code in (CTRL-V or CMD-V)

Then end it with a:  [/code] (again leave out the space).

The word table is a reserved word.  You cannot use it for your own variables, function names or object name.  If you are using a variable named table , please rename it to something like myTable

Rob

hi, i understand that it’s difficult with words.

Below i put a simplified main.lua 

and below, below my level.lua. i repeat that my problem seems that the event listener is not interpreted but when the code is in one main.lua everything works perfectly.

-------------------------------------------------------------------------------- -- "Grid" -------------------------------------------------------------------------------- local grid = require("level") local space = require("level") local spaceleft = require("level") local spacetop = require("level") local widthscreen = require("level") local heightscreen = require("level") local divx = require("level") local divy = require("level") local row2 = require("level") local cell2= require("level") -------------------------------------------------------------------------------- -- "Event on Grid" -------------------------------------------------------------------------------- grid.touch = function ( self, event ) &nbsp; &nbsp; &nbsp; &nbsp; if event.phase == "began" then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("event.x",event.x, "event.y", event.y) &nbsp; &nbsp; &nbsp; &nbsp; ax=myRound((row2\*event.x)/(widthscreen-spaceleft)) &nbsp; &nbsp; &nbsp; &nbsp; posx=myRound((row2\*character.x)/(widthscreen-spaceleft)) &nbsp; &nbsp; &nbsp; &nbsp; sp=(spacetop\*0.5)+spacetop &nbsp; &nbsp; &nbsp; &nbsp; ay=myRound((cell2\*event.y)/(heightscreen-sp)) &nbsp; &nbsp; &nbsp; &nbsp; posy=myRound((cell2\*character.y)/(heightscreen-sp)) &nbsp; &nbsp; &nbsp; &nbsp; if ax == 0 then &nbsp; &nbsp; &nbsp; &nbsp; ax=1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ay == 0 then &nbsp; &nbsp; &nbsp; &nbsp; ay=1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ay \> cell2 then &nbsp; &nbsp; &nbsp; &nbsp; ay=cell2 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ax \> row2 then &nbsp; &nbsp; &nbsp; &nbsp; ax=row &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if posx == 0 then &nbsp; &nbsp; &nbsp; &nbsp; posx=1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if posy == 0 then &nbsp; &nbsp; &nbsp; &nbsp; posy=1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if posy \> cell2 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; posy=cell2 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if posx \> row2 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; posx=row2 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ax ~= row2 and ax \> posx then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ax = posx + 1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ax ~= 0 and ax \< posx then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ax = posx - 1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ay ~= cell2 and ay \> posy then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ay = posy + 1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ay ~= 0 and ay \< posy then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ay = posy - 1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; local function removeCircle(circleOnappears) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears:removeSelf() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.newImage( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "circle.png", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[ay][ax].x, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[ay][ax].y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears.xScale=0.25 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears.yScale=0.25 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears.alpha=0.4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.scaleTo( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circleOnappears, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {time= 350, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xScale=0.09, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yScale=0.09, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alpha=0.1, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete=removeCircle} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) end end Runtime:addEventListener( "touch", grid ) &nbsp;-------------------------------------------------------------------------------- -- "End"------------------------------------------------------------------------- --------------------------------------------------------------------------------- &nbsp;&nbsp;

level.lua

local grid = {} local gridGame = {} grid[1] = &nbsp;{1, 1, 1, 1, 1, 1, 0, 1, 0 } grid[2] = &nbsp;{1, 1, 1, 0, 1, 1, 0, 1, 0 } grid[3] = &nbsp;{1, 1, 1, 0, 3, 1, 0, 1, 1 } grid[4] = &nbsp;{1, 1, 1, 0, 0, 1, 1, 1, 1 } grid[5] = &nbsp;{1, 1, 1, 1, 1, 1, 0, 1, 0 } grid[6] = &nbsp;{1, 1, 1, 1, 1, 1, 0, 1, 1 } grid[7] = &nbsp;{1, 1, 1, 1, 1, 1, 1, 1, 0 } grid[8] = &nbsp;{1, 1, 1, 1, 1, 1, 0, 1, 1 } grid[9] = &nbsp;{1, 1, 1, 1, 1, 1, 0, 1, 1 } grid[10] = {1, 1, 1, 1, 0, 1, 0, 1, 1 } grid[11] = {1, 1, 1, 1, 2, 1, 0, 1, 1 } grid[12] = {1, 1, 1, 1, 1, 1, 1, 1, 1 } grid[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1 } grid[14] = {1, 1, 1, 1, 1, 1, 1, 1, 1 } local row = #grid local row2=9 local widthscreen = 320 local heightscreen = 480 local cell = #grid[1] local cell2=14 local rectx = 30 local recty = 30 local gridscaley = 0.35 local gridscalex = 0.35 local space = 2 local spaceleft =35 local spacetop = 20 local divx=widthscreen/(row\*2) local divy=heightscreen/(cell\*2) for i = 1, row do &nbsp; gridGame[i] = {}; &nbsp; &nbsp; for k = 1, cell do &nbsp; &nbsp; &nbsp; if grid[i][k] == 1 then &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k] = display.newImage("im1.png") &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].yScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].xScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].alpha = 1 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].x = (k - 1) \* (rectx + space) + spaceleft &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].y = (i - 1) \* (recty + space) + spacetop &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; if grid[i][k] == 0 then &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k] = display.newImage("im2.png") &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].yScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].xScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].alpha = 1 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].x = (k - 1) \* (rectx + space) + spaceleft &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].y = (i - 1) \* (recty + space) + spacetop &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;if grid[i][k] == 2 then &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k] = display.newImage("im3.png") &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].yScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].xScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].alpha = 1 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].x = (k - 1) \* (rectx + space) + spaceleft &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].y = (i - 1) \* (recty + space) + spacetop &nbsp; &nbsp; end &nbsp; &nbsp; if grid[i][k] == 3 then &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k] = display.newImage("im4.png") &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].yScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].xScale=0.35 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].alpha = 1 &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].x = (k - 1) \* (rectx + space) + spaceleft &nbsp; &nbsp; &nbsp; &nbsp; grid[i][k].y = (i - 1) \* (recty + space) + spacetop &nbsp; &nbsp; end &nbsp;&nbsp; &nbsp; &nbsp; end end return grid, space, spaceleft, spacetop, divx, divy, row2, cell2

Have you an idea ?

I believe the problem is the following - but I could be wrong: you seem to be using the “table listener” system, but on the runtime instead of an object. Try this:

Runtime:addEventListener( "touch", grid.touch )

instead of

Runtime:addEventListener( "touch", grid )

Also, your big “require” block at the top seems like a big overkill, or even just plain wrong. You should write:

local grid local space local spaceleft local spacetop local divx local divy local row2 local cell2 grid, space, spaceleft, spacetop, divx, divy, row2, cell2 = require("level")

And by the way, widthscreen and heightscreen are not returned by the require. That’s not needed either, because you can calculate them from the other values, it seems.

Ah, and this may be because you simplified the code, but the variable “gridGame” seems to be irrelevant in your code.

Hi Thomas6  :slight_smile:

Thanks for yours advices, it’s solve my problem.

I have an another question, what is the good syntax to work with scene and level on a separated file ?

Is this documentation appropriated to me ? have you a another tip ?

http://docs.coronalabs.com/api/library/storyboard/

Hi,

Good syntax? Good question! I’m sure storyboard is a very good option, and it’s well documented and supported on the forum, so I would advise you to tackle this. Me personally, I don’t like to use pre-fab option, but it’s a very personal thing. Can’t say I have a real good reason for this, except that I do like to have total control and total understanding of what’s going on under the hood.

What I typically do is build all of my separate “screens” (like menu, game, highscore etc…) in separate modules. Each module has a function to build the screen, and one to destroy the screen. Then based on action or input in one module, I call a function to destroy all of the graphics on screen for the first module, and then call up the function to build the screen in another module.

I would advise you to read a lot about modules and requiring for the moment. Bear in mind that you can require a module in several different modules, and the cool thing is that the module will still only load into memory once!