Wish I had the time, because unless you’re doing something “weird” the level lock thing is fairly easy to implement. Where most people run into problems is thinking the problem is locking and unlocking a level, but that’s not what you really want.
You may not want to work on it any longer yourself, but I’m waiting for a download so I’ll just keep typing…
Look at this:
[lua]
local level1Locked = false
local level2Locked = false
local level3Locked = true
local level4Locked = true
[/lua]
Can you tell me which levels are locked? Of course you can, Levels 3 and 4 are locked. You don’t need anything else. The missing key is that you look at those “flags” to determine whether you allow a user to select that level or not – there is no actual “locking” of a level.
Of course, we don’t actually want a separate flag variable for each level, that’s just to make it really visible as to what’s happening. Instead, you’d create a table like this:
[lua]
local levelLocked = { false, false, true, true }
[/lua]
The elements of that table correspond to the levels – first element is whether level 1 is locked, last element is whether level 4 is locked, etc.
So now when you’re putting up buttons where the player can select a level, your code looks something like this:
[lua]
for x = 1, NumLevels do
if levelLocked[x] then
(put up a lock image)
else
(put up the button image so they can select this level)
end
end
[/lua]
You don’t have to worry about whether a “level is locked” because you never allow the user to choose one they shouldn’t have access to.
What about when they finish Level 2 and now Level 3 should be unlocked? You change the “flag” in the table at the end of Level 2 – when they win/finish the level just do this:
[lua]
levelLocked[3] = false
[/lua]
You can just send them straight to the next level or, if they’re going back to the level select screen, now the lock won’t be on that level because the flag has been changed.
Actually, in that last line of code I did something you do NOT want to do – I hardcoded the 3 in there. Instead, it makes more sense to create a global variable that holds the current level. Something like:
[lua]
currLevel = 1 – the starting level
[/lua]
Now when we finish one level and want to go to the next one we can do something like this:
[lua]
currLevel = currLevel + 1
– probably want to check here to see if they’ve done all levels
levelLocked[currLevel] = false
loadLevel(currLevel) – now go load the next level to play
[/lua]
You can use something like GGData to save and load that levelLocked table so it’s persistent across sessions.
Summary – You don’t lock and unlock levels, you set a true/false flag for each level and that tells you whether to allow the user to play it or not.