How to make levels

I wanna know about how to make a game level(seriosly there are really less tutorials for corona sdk) …i mean that where we have to keep these game files (in config.lua or gamelevel.lua)…and for example i have a button which show start and when i click it i want to go to the levels than how do game know that we have to load there…Please tell as i am newbie and want to be good with your help…

thank you in advance

You are Awesome

Levels are stored in .lua files, all you need is create an onClick function that when you click on it you are taken to the game using this line:

composer.gotoScene("blahblah.lua")

You would need to implement composer:

local composer = require(composer)

One of the reasons why there are few tutorials on this subject has to do with it’s a very broad topic with many possible solutions. Every developer will want to do things a certain way.

Now keeping this on the most general of terms, how you do this depends on the game you’re building. You can have one (or a few) game.lua files that are data driven. For instance the game I’m currently building is a side-scrolling sci-fi shooter. All levels are basically the same. I use the same code to detect collisions, all that changes are the images I use and some per-enemy AI logic that rotates through the different levels etc. I only have one “game.lua” file. It knows for level 1 to use Enemy 1. For level 2 use Enemy 2 and Boss 2. At this point you can store your game data in their own .lua files that is pretty much just a table.

Here’s an example: – shiptypes.lua

local shipTypes = {} shipTypes[1] = { title = "Fighter Mk I", name = "fighter1", width = 115, height = 46, health = 100, fireSpeed = 300, healthBays = 2, bombBays = 3, guns = 1, buy = 1000, sell = 800 } shipTypes[2] = { title = "Fighter Mk II", name = "fighter2", width = 133, height = 49, health = 125, fireSpeed = 250, healthBays = 2, bombBays = 2, guns = 1, buy = 2000, sell = 1600 } shipTypes[3] = { title = "Fighter Mk III", name = "fighter3", width = 112, height = 58, health = 150, fireSpeed = 200, healthBays = 3, bombBays = 3, guns = 1, buy = 4000, sell = 3200 } shipTypes[4] = { title = "Fighter Mk IV", name = "fighter4", width = 105, height = 72, health = 175, fireSpeed = 300, healthBays = 3, bombBays = 4, guns = 2, buy = 8000, sell = 6400 } shipTypes[5] = { title = "Warrior Mk I", name = "warrior1", width = 137, height = 58, health = 200, fireSpeed = 250, healthBays = 4, bombBays = 5, guns = 2, buy = 12000, sell = 9600 } shipTypes[6] = { title = "Warrior Mk II", name = "warrior2", width = 125, height = 53, health = 225, fireSpeed = 200, healthBays = 4, bombBays = 6, guns = 2, buy = 24000, sell = 19200 } shipTypes[7] = { title = "Warrior Mk III", name = "warrior3", width = 143, height = 53, health = 250, fireSpeed = 300, healthBays = 5, bombBays = 7, guns = 3, buy = 48000, sell = 38400 } shipTypes[8] = { title = "Warrior Mk IV", name = "warrior4", width = 144, height = 64, health = 275, fireSpeed = 250, healthBays = 5, bombBays = 8, guns = 3, buy = 96000, sell = 76800 } return shipTypes

Now where I need this data, I’ll just do:

local shipTypes = require(“shiptypes”)

and I have access to the table. If I’m level 3 and I need ship 5, I can grab the file name for shipTypes[5] and it’s width and height and feed that to a display.newImageRect(). The code is the same regardless of the level.

The opposite side of the spectrum is the game where each level has so much unique art and code that you can’t reuse anything. I did a kids game where each level was it’s own activity. Coloring book here, puzzle there, action activity etc. It had dozens of scenes and I couldn’t use hardly any code from any scene. In this case, I had dozens of very specific .lua files in my project.

You may find you’re game is somewhere in between. In programming there is a principle called “DRY” – Don’t repeat yourself. If you have 20 levels and you repeat the same collision detection function in each and you have to change one due to a bug you have to make that change 20 times. A good programmer will learn how to compartmentalize common code into libraries to minimize these changes. Using Object Oriented Programming principles you can reduce your dependence on writing the same code multiple times.

If you’re building a Candy Crush like game, perhaps you use a single game.lua, create a level1.lua that has the game map in it and the object could have specific code for that level. It’s a matter of your game and how much code is shared with each level on which way to proceed.

Rob

Hi Rob, do you think when your done with your game you could post the code somewhere? It would really help me out with a lot of things. When your done, that is…

Below is a page with a wealth of info and tutorials for Corona:

http://www.tandgapps.co.uk/resources/coronalabs-resources/

I’ll consider it. I generally don’t open source/public domain my apps. I tend to share snippets from them though. Besides, my punch-list is two pages in a 2 column note book.  I just need to bear down and get some work done on it. :slight_smile:

Rob

Ya thats what i asked…but according to your answer i want to ask you that do we have to save the file for game levels i normal main.lua or we have to make one new lua file…and yes i asked that how to make levels but u answered for how to go to that level …thanks well but tell me how is level made and is it a function…please give example…

YOU ARE AWESOME THANK YOU :slight_smile:

YA ya you can include it where you want to but i want to ask you that how game levels are maked…like if we want to make any function we do like 

local function Example (event) if elseif else end end end Runtime:addEventListener

and if we want to insert image than we do it like 

local image = display.newImage("example.png") image.x=centerX image.y=centerY

but now, for example i have a button which displays “play” and i want to make it like when user touches it it show levels so i ll do

local function onTouch(event) if (event.target.name=="play") then audio.play(backgroundmusic), composer.gotoscene(example.lua) end end Runtime:addEventListener 

Its just what i feel will look like, so if its not correct then correct it, now coming back to point i want to know how levels are made like is it a function or what please give the world most easiest example of the code from the top so i can understand easily(from inserting images to level to going to scene)…Thanx

YOU Are GENIOUS :slight_smile:

If you need advanced levels with varying graphics, physics, and/or animations, I highly recommend that you take a look at Level Director X. It’s rather advance in functionality but easy for anyone to pick up and use. There is a completely free version and the full version is only $20 (a steal if you ask me).

http://www.retrofitproductions.com/

http://www.retrofitproductions.com/level-director-x/

Now that you’re asking about how to manage multiple scenes, we have a wealth of tutorials, guides and documentation on Composer our scene manager. But let me point you to one resource in particular:
 

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

At the bottom is a link to our Github repository where you can download the basic game template. It has no game in it other than two buttons that say “I Win” and “I loose” or something like that. The tutorial explains the flow of the game template and it serves as a great example of composer scenes and moving among them.  I would suggest starting there.

After that, check out:

https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

It has a collection of (almost all) all of our information regarding Composer.

Rob

Oh so you means that whenever i click on the button “play”…it changes the scenes? and it doesnot have any link to function?

When you say link to function, are you thinking like a web link or do you have another thought in mind.  With Corona going to a new scene is a matter of calling the command:   composer.gotoScene( “scenename”, { optional=parameters } ) where “scenename” is the name of the .lua file containing a valid Composer scene that you want to go to and the optional parameters include things like what transition to use and how long the transition should take. For instance if you wanted to crossFade from menu.lua to game.lua over 500 milliseconds (1/2 second), you would write:

composer.gotoScene( “game”, { time=500, effect=“crossFade” } )

Now how you get to a point to run that command is dependent on your app and what’s going on. Going from “Menu” to “Game”, the menu scene probably has a “Play” button. When that button is pressed an event is generate that will call a function you’ve defined to handle the button touch. In that button touch at a minimum you want to call the command above.

Once you’re in your game and something happens like your player dies and you want to go to the game over scene you might just call the composer.gotoScene( “gameover” ) as part of the code that detected the player died or if the player beat the level and you want to go to the next level you might want a “nextlevel” scene that you go to automatically.

Rob

OH Thats great but i have a little confusion that is when i do not know about composer screen then i  thought that moving from play button to game it maybe like this->

display.setStatusBar (display.HiddenStatusBar) local backgroundmusic = audio.loadStream("background.mp3") local centerX = display.contentCenterX local centerY = display.contentCenterY local background = display.newImage("background.png") background.x = centerX background.y = centerY local button = display.newImage("button.png") button.x = centerX button.y = 420 --Now this is weird when i done this local function onTouch(event) if(event.phase == "began") then local newImage =display.newImage("nyc.png") newImage.x = centerX newImage.y= centerY audio.play(backgroundmusic) end end Runtime:addEventListener("touch", onTouch)

So is like going to scene or what as when i click on the screen ,the new image appear and a background music plays…for example if it is in composer scene then how do it be held…i mean that where we have to keep these files!!!..

Thanx and your tutorials are cool!

Depending on the complexity of the game, you may not need scenes at all. For something simple you can build everything in main.lua. But once things get more complex it’s really best to break things out into multiple modules. My first game was all in main.lua before I learned about scene management and today that code is nearly impossible to work on. The file is too long, too many lines of code in one file. Breaking things out into their own modules helps maintain the project and makes it easier to work with.

To answer your last question: think of a scene as like it’s own mini-app. It will have it’s own background, buttons, artwork, activity and so on. Each scene is a page of your app if you want to think about it in web page terminology. Each scene could have it’s own background music.

If you have not done so, download the sample-game-template from the first tutorial. It will create a folder with everything in it. Your project folder has a main.lua, config.lua and build.settings. Your scene.lua files go in the same folder. As you learn more, you can with a few simple changes have your scene files in a sub-folder,  but I would wait on that for a couple of projects. The main focus when starting to use Composer is understanding Composer.

I would also suggest that you go through and build out the two apps in our Getting Started Guide. 

https://docs.coronalabs.com/guide/programming/index.html

Chapter 5 covers how to get a game that’s all in your main.lua into scenes. I would still recommend going through the previous chapters so you’re familiar with the game code you will be converting.

Rob

Oh thank you very much and yes please tell me the name of the game you maked and got the highest download among all your other game…in simple words tell me your most successful name of the game!..Thanks and ya on android one!

Normally, you would make a new lua file. But, like Rob said, it is very useful to transfer data that you are going to use multiple times from the same source.

local function onTouch(event) if (event.target.name == "play") then composer.gotoscene("example.lua") end end 

For the background music you would want to play it when you get to the next scene.

Oh Ya! its simple and bro please reply to my latest posts! Thanx

Levels are stored in .lua files, all you need is create an onClick function that when you click on it you are taken to the game using this line:

composer.gotoScene("blahblah.lua")

You would need to implement composer:

local composer = require(composer)

One of the reasons why there are few tutorials on this subject has to do with it’s a very broad topic with many possible solutions. Every developer will want to do things a certain way.

Now keeping this on the most general of terms, how you do this depends on the game you’re building. You can have one (or a few) game.lua files that are data driven. For instance the game I’m currently building is a side-scrolling sci-fi shooter. All levels are basically the same. I use the same code to detect collisions, all that changes are the images I use and some per-enemy AI logic that rotates through the different levels etc. I only have one “game.lua” file. It knows for level 1 to use Enemy 1. For level 2 use Enemy 2 and Boss 2. At this point you can store your game data in their own .lua files that is pretty much just a table.

Here’s an example: – shiptypes.lua

local shipTypes = {} shipTypes[1] = { title = "Fighter Mk I", name = "fighter1", width = 115, height = 46, health = 100, fireSpeed = 300, healthBays = 2, bombBays = 3, guns = 1, buy = 1000, sell = 800 } shipTypes[2] = { title = "Fighter Mk II", name = "fighter2", width = 133, height = 49, health = 125, fireSpeed = 250, healthBays = 2, bombBays = 2, guns = 1, buy = 2000, sell = 1600 } shipTypes[3] = { title = "Fighter Mk III", name = "fighter3", width = 112, height = 58, health = 150, fireSpeed = 200, healthBays = 3, bombBays = 3, guns = 1, buy = 4000, sell = 3200 } shipTypes[4] = { title = "Fighter Mk IV", name = "fighter4", width = 105, height = 72, health = 175, fireSpeed = 300, healthBays = 3, bombBays = 4, guns = 2, buy = 8000, sell = 6400 } shipTypes[5] = { title = "Warrior Mk I", name = "warrior1", width = 137, height = 58, health = 200, fireSpeed = 250, healthBays = 4, bombBays = 5, guns = 2, buy = 12000, sell = 9600 } shipTypes[6] = { title = "Warrior Mk II", name = "warrior2", width = 125, height = 53, health = 225, fireSpeed = 200, healthBays = 4, bombBays = 6, guns = 2, buy = 24000, sell = 19200 } shipTypes[7] = { title = "Warrior Mk III", name = "warrior3", width = 143, height = 53, health = 250, fireSpeed = 300, healthBays = 5, bombBays = 7, guns = 3, buy = 48000, sell = 38400 } shipTypes[8] = { title = "Warrior Mk IV", name = "warrior4", width = 144, height = 64, health = 275, fireSpeed = 250, healthBays = 5, bombBays = 8, guns = 3, buy = 96000, sell = 76800 } return shipTypes

Now where I need this data, I’ll just do:

local shipTypes = require(“shiptypes”)

and I have access to the table. If I’m level 3 and I need ship 5, I can grab the file name for shipTypes[5] and it’s width and height and feed that to a display.newImageRect(). The code is the same regardless of the level.

The opposite side of the spectrum is the game where each level has so much unique art and code that you can’t reuse anything. I did a kids game where each level was it’s own activity. Coloring book here, puzzle there, action activity etc. It had dozens of scenes and I couldn’t use hardly any code from any scene. In this case, I had dozens of very specific .lua files in my project.

You may find you’re game is somewhere in between. In programming there is a principle called “DRY” – Don’t repeat yourself. If you have 20 levels and you repeat the same collision detection function in each and you have to change one due to a bug you have to make that change 20 times. A good programmer will learn how to compartmentalize common code into libraries to minimize these changes. Using Object Oriented Programming principles you can reduce your dependence on writing the same code multiple times.

If you’re building a Candy Crush like game, perhaps you use a single game.lua, create a level1.lua that has the game map in it and the object could have specific code for that level. It’s a matter of your game and how much code is shared with each level on which way to proceed.

Rob