Unlock levels in Android logic
I have a level scene allowing the user to select a level.
But to go to the next level, the user must complete the level before it.
Right now, the first level is automatically unlocked.
When it's complete, I want to unlock the second level. and when the second level is completed, open the third level.
I cannot think how I will do this. i was thinking about sharedpreferences but dont know what logic i should use to do this.
I could use some help on the logic behind this.
Can anyone help me?
EDIT:
In my game scene, when the game is over I call this sharedPreference
editor.putString("level_completed"+level, "unlocked");
editor.commit();
Then I do ..
String levelStatus = preference.getString("level_completed:"+level, "locked");
if(levelStatus.equals("unlocked")){
}else{
if(level == 0){
box.setUserData("unlocked");
}else{
box.setColor(1.0f,0.0f,0.0f);
box.setUserData("locked");
}
}
in the level select scene to check if the level is unlocked.
This doesn't seem to work well. The level to be unlocked is not unlocked.
source to share
There are several ways in which you could approach it. If the progression is always linear (1, 2, 3, 4), you can simply store an integer for the "max unlocked level" and then update it when you finish the level. Otherwise you can also have a lot of booleans like "level 2 unlocked" and when you finish level 1 you can set "level 2 unlocked" to true. General preference is definitely the smartest way to go.
source to share
What I would do is look at it from an object oriented perspective, which is what Java does. When I get stuck on a piece of code, I write it down. Think what is the purpose of the code.
-
You probably have a class called Level. In this level class you can have the property "unlocked" or vice versa "locked". Then, when the user goes to the "play level" screen or whatever you have, allow the user to see the unlocked levels.
-
You probably also have a Player class, so you might have an "unlockedLevels" property that is of type Level []. So, all unlocked levels will be in this array of Levels.
It's all about how your code is structured, structure and the true orientation of objects are the most important things in Java. When you stray from object-oriented design, you can run into problems that should never exist.
source to share
At some point, if your game doesn't restart from the start every time the user does a little something, you save the game state. You save the user's account, game state, etc. When you do, however you do, save the unlocked levels as well. It's just one bit of information per level, so you don't have to be smart about it. Perhaps you also have a "save" button, save files, the ability to load from save. Again, just drop the user level when you keep everything else.
Or haven't you started saving yet?
Well, start by storing the state in packages defined for activity lifecycle methods like onPause. If several saves are suitable for your game, go with the files and decide their structure. If only one "save" is done, the preference is likely to be very good for you. Test for this: if you ever want to calculate a preference key, don't use preferences.
source to share