level unlocking

the code below is for a level select menu, my questionis how would i call this so that, for example, i beat level one, how would i call this in level one to change the levels table so that instead of showing a locked screen, it sunlocks?

would it be something like, levels[1]=3 levels[2]=1 where 1 is unlocked and 3 is completed? im not sure how to calling this code file into my level one

local composer= require(“composer”)

local scene = composer.newScene()

local widget = require(“widget”)

local levelsave = require(“levelSave”)

levels = {

{1,2,2,2,2,

2,2,2,2,2,

2,2,2,2,2,}

}

 levels = loadInfo() 

images ={

{ getFile=“unlock.png”, types = “play” },

{ getFile=“lock .png”,types = “locked” },

{ getFile=“greenchecked.png”, types = “done” }

}

local function buttonPress(event)

composer.gotoScene(event.target arrow-10x10.png.destination,{effect=“fade”})

print(event.target.destination)

return true

end

function scene:create(event)

local sceneGroup = self.view

local background = display.newImage( “title1.2.png”)

background.x = display.contentCenterX

background.y = display.contentCenterY

sceneGroup:insert(background)

local clouds = display.newImage( “clouds.png”)

clouds.x = 0

clouds.y = centery

sceneGroup:insert(clouds)

local c2 arrow-10x10.png = display.newImage( “clouds.png”)

c2 arrow-10x10.png.x = 600

c2 arrow-10x10.png.y = 210

sceneGroup:insert(c2)

function scrollClouds(self,event)

if self.x < -540 then

self.x = 540

else

self.x=self.x-.5

end

end

clouds.enterFrame = scrollClouds

Runtime:addEventListener(“enterFrame”,clouds)

c2 arrow-10x10.png.enterFrame = scrollClouds

Runtime:addEventListener(“enterFrame”,c2 arrow-10x10.png)

local levelIndex=0

for i=0,2 do

for j=1,5 do

tablePlace = i*5+j

levelIndex = levelIndex+1

local imagesId = levels[levelIndex]

levelImg = display.newImageRect(images[imagesId].getFile, 45,45)

levelImg.x = 5+(j*55)

levelImg.y = 85+(i*65)

sceneGroup:insert(levelImg)

levelTxt = display.newText("level  "…tostring(tablePlace),0,0,“Helvetica”, 10)

levelTxt.x = 5 +(j*55)

levelTxt.y = 120+(i*65)

levelTxt:setTextColor(250,255,251)

sceneGroup:insert(levelTxt)

levelImg.destination = “level0”…tostring(tablePlace)

if images[imagesId].types ~= “locked” then

levelImg:addEventListener(“tap”,buttonPress)

end

end

end

local title = display.newImage(“ll.png”)

title.x = centerx

title.y = display.screenOriginY + 30

sceneGroup:insert(title)

local backBtn = display.newImage(“back.png”)

backBtn.x = display.screenOriginX +50

backBtn.y = display.contentHeight -20

backBtn.destination = “menu”

backBtn:addEventListener(“tap”,buttonPress)

sceneGroup:insert(backBtn)

end

function scene:show(event)

local sceneGroup = self.view

saveInfo()

end

function scene:hide(event)

local sceneGroup = self.view

end

function scene:destroy(event)

local sceneGroup = self.view

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”,scene)

scene:addEventListener(“hide”,scene)

scene:addEventListener(“destroy”, scene)

return scene

Hi @duracakn10,

Going back to your primary question, there are virtually any number of ways you could store data for each level. Personally, I would recommend just populating a main “levels” table with a bunch of sub-tables, and each sub-table has a set of key-value pairs, for example:

[lua]

local levels = {

   { unlocked=true, name=“The Shire” },

   { unlocked=false, name=“Rivendell” },

   { unlocked=false, name=“Mount Doom” },

}

[/lua]

This way, you have every level easily accessible by its index number, and by using key-value pairs, you can add any amount of additional information to each sub-table (for example, maybe you need to store that level’s high score, awards earned in that level, etc.).

Then, accessing a bit of info just involves drilling down into that sub-table:

[lua]

print( levels[1].unlocked )

print( levels[1].name )

– etc.

[/lua]

Hope this helps,

Brent

local composer= require(“composer”)

local scene = composer.newScene()

local widget = require(“widget”)

local levelsave = require(“levelSave”)

levels = {

{1,2,2,2,2,

2,2,2,2,2,

2,2,2,2,2,}

}

 levels = loadInfo() 

images ={

{ getFile=“unlock.png”, types = “play” },

{ getFile=“lock .png”,types = “locked” },

{ getFile=“greenchecked.png”, types = “done” }

}

local function buttonPress(event)

composer.gotoScene(event.target arrow-10x10.png.destination,{effect=“fade”})

print(event.target.destination)

return true

end

function scene:create(event)

local sceneGroup = self.view

local background = display.newImage( “title1.2.png”)

background.x = display.contentCenterX

background.y = display.contentCenterY

sceneGroup:insert(background)

local clouds = display.newImage( “clouds.png”)

clouds.x = 0

clouds.y = centery

sceneGroup:insert(clouds)

local c2 arrow-10x10.png = display.newImage( “clouds.png”)

c2 arrow-10x10.png.x = 600

c2 arrow-10x10.png.y = 210

sceneGroup:insert(c2)

function scrollClouds(self,event)

if self.x < -540 then

self.x = 540

else

self.x=self.x-.5

end

end

clouds.enterFrame = scrollClouds

Runtime:addEventListener(“enterFrame”,clouds)

c2 arrow-10x10.png.enterFrame = scrollClouds

Runtime:addEventListener(“enterFrame”,c2 arrow-10x10.png)

local levelIndex=0

for i=0,2 do

for j=1,5 do

tablePlace = i*5+j

levelIndex = levelIndex+1

local imagesId = levels[levelIndex]

levelImg = display.newImageRect(images[imagesId].getFile, 45,45)

levelImg.x = 5+(j*55)

levelImg.y = 85+(i*65)

sceneGroup:insert(levelImg)

levelTxt = display.newText("level  "…tostring(tablePlace),0,0,“Helvetica”, 10)

levelTxt.x = 5 +(j*55)

levelTxt.y = 120+(i*65)

levelTxt:setTextColor(250,255,251)

sceneGroup:insert(levelTxt)

levelImg.destination = “level0”…tostring(tablePlace)

if images[imagesId].types ~= “locked” then

levelImg:addEventListener(“tap”,buttonPress)

end

end

end

local title = display.newImage(“ll.png”)

title.x = centerx

title.y = display.screenOriginY + 30

sceneGroup:insert(title)

local backBtn = display.newImage(“back.png”)

backBtn.x = display.screenOriginX +50

backBtn.y = display.contentHeight -20

backBtn.destination = “menu”

backBtn:addEventListener(“tap”,buttonPress)

sceneGroup:insert(backBtn)

end

function scene:show(event)

local sceneGroup = self.view

saveInfo()

end

function scene:hide(event)

local sceneGroup = self.view

end

function scene:destroy(event)

local sceneGroup = self.view

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”,scene)

scene:addEventListener(“hide”,scene)

scene:addEventListener(“destroy”, scene)

return scene

Hi @duracakn10,

Going back to your primary question, there are virtually any number of ways you could store data for each level. Personally, I would recommend just populating a main “levels” table with a bunch of sub-tables, and each sub-table has a set of key-value pairs, for example:

[lua]

local levels = {

   { unlocked=true, name=“The Shire” },

   { unlocked=false, name=“Rivendell” },

   { unlocked=false, name=“Mount Doom” },

}

[/lua]

This way, you have every level easily accessible by its index number, and by using key-value pairs, you can add any amount of additional information to each sub-table (for example, maybe you need to store that level’s high score, awards earned in that level, etc.).

Then, accessing a bit of info just involves drilling down into that sub-table:

[lua]

print( levels[1].unlocked )

print( levels[1].name )

– etc.

[/lua]

Hope this helps,

Brent