This has been giving me a headache for about two days now!
I have a game with levels. When the level selection scene is loaded, i check the MaxLevelReached int variable.
The first time the user plays, the maxlevelreached variable is 0.
The first level is = 0 so what i do is this when the scene is loaded…
private int level = 60;
if (level >= maxLevelReached || maxLevelReached == 0){
box.setColor(0, 0, 0);
}
else
{
box.setColor(0, 0.9f, 0);
}
As you see I check to see if the level is less than or equal to the maxLevel.
So to start off the user should be able to play to the first level, and then when it is completed the second level will be unlocked. I know this seems really simple, but for some reason I’ve been struggling with it.
If the user clicks on a level and it Isn’t unlocked yet, this is how i test to start the level or not..
Log.e("Level:"+levelClicked, "Level");
if(levelClicked >= maxLevelReached){
levelClicked = levelToLoad;
Intent intent = new Intent(level.this, GameLevel.class);
intent.putExtra("level", levelClicked);
startActivity(intent);
}
This Isn’t working either… For some reason all of the levels are playable when it should only be the first level to begin with.
Here is how I unlock the levels once the previous level is completed..
int CurrentLevel = level+1;
Log.e("Current Level unlocked", String.valueOf(CurrentLevel));
editor.putInt("max_level",CurrentLevel);
editor.commit();
Then I do this in the level selector scene…
int unlockedLevel = preference.getInt("max_level",0);
maxLevelReached += unlockedLevel;
I know this seems like a mess. Indeed I believe that its giving me a headache. Could anyone help me out please?
I think your comparison is wrong.
This reads, if the level the user is on is greater than the max level reached or the max level reached is level zero then allow.
So it should be
<instead.