coder_For_Life22
coder_For_Life22

Reputation: 26991

unlocking levels in Android Game logic

I have a level scene that i allow the user to select a level.

But to move on to the next level the user must complete the level before it.

Right now the first level which is automatically unlocked.

When it is completed i want unlock the second level. and when the second level is completed, unlock the third level.

i cant think of how i would go about doing this. i thought of sharedpreferences but dont know the logic i should use for this.

i could use some help on the logic behind this.

Any can help me out?

EDIT:

In my gameplay scene when is game is completed i call this to 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 selection scene to check if the level is unlocked.

This doesnt seem to be working to well. A level that should be unlocked isnt unlocked.

Upvotes: 4

Views: 3761

Answers (3)

Julian Fondren
Julian Fondren

Reputation: 5609

At some point, unless your game restarts from the beginning every time the user does something else for a bit, you save the state of the game. You save the user's score, the state of the game, etc. When you do this, however you do this, save also the unlocked levels. It's only one bit of information per level, so you don't need to be clever about this. Perhaps you also have a 'save' button, save files, the ability to load from a save. Again, just throw the user's level unlocks in when you're saving everything else.

Or have you not begun to save anything, yet?

Well, start by saving state in the bundles given to activity lifecycle methods, like onPause. If multiple saves are appropriate for your game, go with files and decide on their structure. If only one 'save' will do, preferences are likely to work just fine for you. A test for that: if you ever want to calculate the key of a preference, don't use preferences.

Upvotes: 2

John
John

Reputation: 3807

What i would do is look at it in an Object Oriented Perspective, which is what Java is all about. When i get stuck on a piece of code then i write it down. Think of what the objective of the code is.

  • You probably have a class called Level. In this level class you could have a property called "unlocked" or the opposite "locked". Then when the user goes to your "play a level" screen or what have you, then only allow the user to see the levels that are unlocked.

  • You also probably have a Player class of some sort so you could have a property called "unlockedLevels" which is of the type Level[]. So then all of the unlocked levels would be in this array of Levels.

It is all about how your code is structured, structure and true Object Orientation are the most important things in Java. When you stray from Object Oriented Design then you can run into problems that should never have existed.

Upvotes: 2

kabuko
kabuko

Reputation: 36302

There are a number of ways you could approach it. If the progression is always linear (1, 2, 3, 4) then you could just store an integer for "max unlocked level" and then update it whenever you complete a level. Otherwise, you could also have many booleans like "level 2 unlocked" and when you complete level 1, you can set "level 2 unlocked" to true. Shared Preferences is definitely a reasonable way to go.

Upvotes: 3

Related Questions