Hi guys,
I have a (new) problem unlocking levels. I asked the community a couple of questions on the matter before and they´ve been great in solving them, so I thought I´d try my luck again.
I´ll try and explain the problem as good as I can:
Let´s say I´m playing level 5, where I reach the win condition and unlock level 6. However, when I refresh or restart the game, and play level 3 again and reach the win condition, level 7 is unlocked (being the one after the last level played), which is not supposed to happen.
This is my code:
– menu.lua file, containing the logic to make the unlocked level playable.
[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
–Check to see if a file for the current level already exists, if it
–doesn’t then create it and set the current level to 1.
local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
unlocked = loadFile(“unlocked.txt”)
–currentLevel = “empty”
if currentLevel == “empty” then
currentLevel = 1
unlocked = 1
saveFile(“currentLevel.txt”, currentLevel)
saveFile(“unlocked.txt”, unlocked)
end
end
checkForFile()
–Table for levels
local level = {}
local function onSceneTouch(event )
if event.phase == “ended” then
storyboard.gotoScene( event.target.scene )
return true
end
end
local startX = 45
function scene:createScene( event )
local localGroup = self.view
–Load level icons accordingly
function setupLevels()
for i = 1, 10 do
if tonumber(unlocked) >= i then
level[i] = display.newCircle( startX*i, 50, 20 )
level[i]:setFillColor(220, 220, 120)
localGroup:insert( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“touch”, onSceneTouch)
elseif tonumber(unlocked) < i then
level[i] = display.newImageRect(“images/lock.png”,50,50)
level[i]:setFillColor(255, 255, 255)
level[i].x = startX*i
level[i].y = 50
localGroup:insert( level[i] )
end
end
end
setupLevels()
Reset = display.newImageRect(“images/reset.png”, _W/10,75)
Reset:setReferencePoint(display.CenterReferencePoint)
Reset.x = _W-150
Reset.y = _H-100
Reset.scene = “refresh”
localGroup:insert(Reset)
end
function scene:enterScene( event )
local prior_scene = storyboard.getPrevious()
storyboard.purgeScene( prior_scene )
Reset:addEventListener(“touch”, onSceneTouch)
end
scene:addEventListener( “enterScene”,scene )
scene:addEventListener( “createScene”, scene )
– local function resetData(event)
– saveFile(“currentLevel.txt”, 1)
– end
–resetData()
return scene[/lua]
– level 3, the logic unlocking the next level.
Some additional info: the level3unlocked boolean is declared false in the main.lua
In the current logic the “unlocked = unlocked + 1” state is obviously what is causing the problem. It adds 1 to the last level unlocked. However this logic was needed to solve another problem where when you unlocked level 5 and went back to play level 2, level 3 unlocked as was supposed to but 4 and 5 closed again.
[lua]local function checkWin ()
if ballsIn == 1 then
if level3unlocked == false then
level3unlocked = true
unlocked = unlocked + 1
saveFile(“currentLevel.txt”, 4)
saveFile(“unlocked.txt”, unlocked)
end
end
end
Runtime:addEventListener(“enterFrame”, checkWin)[/lua]
I need to figure out some way to save the last level played and make sure that one is the only one where the unlocked = unlocked +1 logic is applied.
Any ideas ?
Thanks,
JA
[import]uid: 126207 topic_id: 30926 reply_id: 330926[/import]
[import]uid: 52491 topic_id: 30926 reply_id: 123726[/import]