Hi Guys,
I have a small problem with unlocking levels. I use Peach Pellen´s EGO (which is great btw) to unlock levels. I´ll try and explain it as good as I can:
Let´s say for instance I play the game and unlock level 4. I close the game (or refresh it) and decide I want to play level 2 again. I reach the desired score, refresh the game and when I come back in, Level 4 is locked again, and only level 3 is unlocked (as the score logic of level 2 triggerd the unlocking of level 3 but failed to “remember” level 4 was also unlocked in a previous session ). Any tips on how to change the logic so that level 4 in this example would remain unlocked even if you go back to play a previous level ?
This is my code:
– Main.lua
[lua]-- Global Vars
_W = display.contentWidth
_H = display.contentHeight
score = 0
velocity = 70
force_multiplier = 4
level1unlocked = false
level2unlocked = false
level3unlocked = false
level4unlocked = false
level5unlocked = false
level6unlocked = false
level7unlocked = false
level8unlocked = false
level9unlocked = false
–Require and setup Ego
ego = require “ego”
saveFile = ego.saveFile
loadFile = ego.loadFile
local storyboard = require “storyboard”
storyboard.gotoScene( “menu” )[/lua]
– Menu.lua
[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”)
if currentLevel == “empty” then
currentLevel = 1
saveFile(“currentLevel.txt”, currentLevel)
end
if level1unlocked == true then
currentLevel = 2
end
if level2unlocked == true then
currentLevel = 3
end
if level3unlocked == true then
currentLevel = 4
end
if level4unlocked == true then
currentLevel = 5
end
if level5unlocked == true then
currentLevel = 6
end
if level6unlocked == true then
currentLevel = 7
end
if level7unlocked == true then
currentLevel = 8
end
if level8unlocked == true then
currentLevel = 9
end
if level9unlocked == true then
currentLevel = 10
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
local function setupLevels()
for i = 1, 10 do
if tonumber(currentLevel) >= 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(currentLevel) < 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]
– Level3.lua, unlock levels logic
[lua]function scene:enterScene( event )
local group = self.view
– to make sure the menu file reads the new state of the boolean levelUnlocked
local prior_scene = storyboard.getPrevious()
storyboard.removeScene( prior_scene )
– Collision event triggering a score update and changing the ballsIn var
function bullet:collision (event)
if event.other.myName == “bar” then
bullet.isLoaded = true
end
if bullet.isLoaded == true and event.other.myName == “basket” and event.phase == “ended” then
score = score + 10
scoreText.text = " " … score
ballsIn = ballsIn + 1
end
end
end
– Unlock level logic when the desired score has been reached
local function checkWin ()
if ballsIn == 1 then
saveFile(“currentLevel.txt”, 4)
level3unlocked = true
end
end
Runtime:addEventListener(“enterFrame”, checkWin)
function scene:exitScene( event )
score = 0
ballsIn = 0
end[/lua] [import]uid: 126207 topic_id: 30500 reply_id: 330500[/import]