Locked, Playable and Compleated levels.

Hi everybody!

Im have a qestion for you guys. How do i make the following feature.

*This is a list of levelbuttons*

Blue button = Locked level

Red button = Playable level

Green button = Compleated level.

All the colors are .PNG files of a button with the color.

My function should look like this:

Levels

1 red
2 blue
3 blue

When i compleate level 1 the screen should looks like this:

1 green
2 red
3 blue

And so on… Well, i guest you get the idea. Then i want it to save everything using ice but, im not that good with ice.

So it would be really good if someone would can tell me how to save states with ice. And how to make the function above. Sorry for bad discription Xd

Link to tutorial, samplecode or and explenation would be really awesome! :slight_smile:

//Philip

[import]uid: 119384 topic_id: 22408 reply_id: 322408[/import]

Use a module called director class. It’s super easy and there is even a video tutorial on Youtube. Just search Director Class on Google.

I guess a button could look like this:

[lua]local button1 = display.newImage(“button1.png”)
button1.x = display.contentWidth/2
button1.y = display.contentHeight/2
button1.scene = “game”

local function changeScene()
director:changeScene(e.target.scene)
end

button1:addEventListener(“touch”, changeScene)[/lua]

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 22408 reply_id: 89328[/import]

Yeah im using director class but i dont know how to make that function above ^^ When you compleate level 1, level 2 will be unlocked and so on. If you know how to to that, please let me know:)

//Philip [import]uid: 119384 topic_id: 22408 reply_id: 89335[/import]

You could use rob’s loadsave.lua script found here - https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Use that to save your current level (and later on any other things you need saved) then when your creating your level buttons you can just check if the level that goes with that button is less then, equal to or greater then your current level and set the colours accordingly.

Here is a little example of how to do that.

require("loadsave")  
   
-- Load the lvlSettings from a .json file  
local checkSettings = loadTable("lvlSettings.json")  
 -- if this is the first time playing we have to set lvlSettings to a value  
 if (checkSettings == nil) then  
 local defaults = {}  
 defaults.curLvl = 3  
 saveTable(defaults, "lvlSettings.json")  
 end  
  
local lvlSettings = loadTable("lvlSettings.json")  
  
-- Print out our current level   
print(lvlSettings.curLvl)  
  
--[[   
Now when setting up the buttons, you just have to see if they have been played or not.  
If they are less then curLvl, they have been completed (Green Button)  
If they are the curLvl, they are playable (Red Button)  
If they are higher then curLvl, they are locked (Blue Button)  
--]]  
  
-- example:  
  
local lvlBtn = {}  
  
 -- create four buttons  
for i = 1, 4 do  
 -- check to see the buttons state compared to its number and the curLvl  
 if i \< lvlSettings.curLvl then -- Lvl is completed  
 lvlBtn[i] = ui.newButton{   
 default = "greenBtn.png",   
 over = "greenBtn.png",   
 text = "Level: "..i,  
 textColor = { 0, 0, 0, 255 },  
 size = 12,  
 align = "center",  
 onRelease = buttonListener  
 }  
 lvlBtn[i].x = display.contentWidth/2  
 lvlBtn[i].y = -50 + (i\*85)  
 lvlBtn[i].state = "Completed"  
 elseif i == lvlSettings.curLvl then -- Lvl is playable  
 lvlBtn[i] = ui.newButton{   
 default = "redBtn.png",   
 over = "redBtn.png",   
 text = "Level: "..i,  
 textColor = { 0, 0, 0, 255 },  
 size = 12,  
 align = "center",  
 onRelease = buttonListener  
 }  
 lvlBtn[i].x = display.contentWidth/2  
 lvlBtn[i].y = -50 + (i\*85)  
 lvlBtn[i].state = "Playable"  
 else -- Lvl isn't unlocked yet  
 lvlBtn[i] = ui.newButton{   
 default = "blueBtn.png",   
 over = "blueBtn.png",   
 text = "Level: "..i,  
 textColor = { 0, 0, 0, 255 },  
 size = 12,  
 align = "center",  
 onRelease = buttonListener  
 }  
 lvlBtn[i].x = display.contentWidth/2  
 lvlBtn[i].y = -50 + (i\*85)  
 lvlBtn[i].state = "Locked"   
 end  
end  

Obviously you would need to change the button images names (or make your match it) and the y/x location of your buttons for how you want or change it to fit more of a system you use now, but this gives you the basic idea of how it can be done.

This same idea could be used with ego and ice also if you wish to use one of those rather for saving. [import]uid: 69700 topic_id: 22408 reply_id: 89340[/import]

I am using ice in the game im currently working on and it works great for exactly what you are looking to do. Here is an example from my game where i am saving a value in ice called “level complete” with a value of 0 for not and 1 for complete.

here is the level file and the ice save function is called when you win and the highscore is higher than the current highscore.

[lua]local c1 = ice:loadBox (“c1”)
c1:storeIfHigher(“lvlHS”,highScore) c1:storeIfHigher(“LvlComplete”, 1)
c1:save()[/lua]

here is a peace of the code from my level select where it will check the ice “c1” file to see if you have completed the level and display the unlock icon or the lock if you have not.
[lua]–Level 18
–Unlocked
if c1: retrieve(“L17Complete”) == 1 then
local lvl18Unlocked = display.newImageRect ( slide2, “levelOpen.png”, 50, 51 )
lvl18Unlocked.x = 720; lvl18Unlocked.y = 50;
lvl18Unlocked.scene = “loadc1L18”
lvl18Unlocked: addEventListener (“tap”, moveScene)
end

–Locked
if c1: retrieve(“L17Complete”) ~= 1 then
local lvl18Locked = display.newImageRect (slide2, “levelLocked.png”, 50, 51 )
lvl18Locked.x = 720; lvl18Locked.y = 50;
end

–Level 19
–Unlocked
if c1: retrieve(“L18Complete”) == 1 then
local lvl19Unlocked = display.newImageRect ( slide2, “levelOpen.png”, 50, 51 )
lvl19Unlocked.x = 800; lvl19Unlocked.y = 50;
lvl19Unlocked.scene = “loadc1L19”
lvl19Unlocked: addEventListener (“tap”, moveScene)
end

–Locked
if c1: retrieve(“L18Complete”) ~= 1 then
local lvl19Locked = display.newImageRect (slide2, “levelLocked.png”, 50, 51)
lvl19Locked.x = 800; lvl19Locked.y = 50;
end[/lua]
I would recommend using ice, great module!
[import]uid: 126161 topic_id: 22408 reply_id: 89351[/import]

There’s a level locking tutorial using Ego on Techority.com as well, although I would still recommend Ice as it is more useful long term. (Ego is for very simple, basic stuff whereas Ice is perfect for various uses.) [import]uid: 52491 topic_id: 22408 reply_id: 89397[/import]

Thanks everybody! But is there a way to add every level-button as a global flag/variable. So i can change it from different screens. And then save it with ice:P?

//Philip [import]uid: 119384 topic_id: 22408 reply_id: 89442[/import]

What do u mean by change it from different screens? [import]uid: 126161 topic_id: 22408 reply_id: 89443[/import]

Ego sample does do it from a different screen using director - look at that sample. Could easily be modified to use with Ice instead of Ego :slight_smile: [import]uid: 52491 topic_id: 22408 reply_id: 89543[/import]

Hi Peach, how would I integrate your level locking tutorial with ICE instead of Ego? A few pointers would be helpful, I think this functionality is the last missing piece of my game! [import]uid: 129334 topic_id: 22408 reply_id: 104342[/import]

Hey Mark, I didn’t create Ice, Graham Ranson did. (He also created Lime.)

You’d want to check out any samples he has up, I think his section in the code exchange has some useful info :slight_smile: [import]uid: 52491 topic_id: 22408 reply_id: 104380[/import]