Você está na página 1de 1

unlocking the next level of game

Asked 4 years ago Active 2 years, 7 months ago Viewed 1k times

I'm making a trivia quiz app with 10 levels , each level is unlocked if the previous level's score is 4 or greater. Now if I use this code
(using shared preferences for storing the game data) it works just fine!
0
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
SharedPreferences pref2 = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
int score2 = pref2.getInt("score2",0); //score2 is the score of previous level
if(score2 >= 4)
{

startActivity(new Intent(Lyricswho.this,Lylevel3.class));
}
else{ Toast.makeText(getApplicationContext(), "Level Locked",
Toast.LENGTH_SHORT).show();}
}
});

however I was trying to implement somthing like I set a lock image imageview on the button and I want it to disappear/remove when the
is score>=4. The problem I'm facing is that I can't put it inside the setOnClickListener function because it will work when the "Level2"
button is pressed that is not right as soon as Level1's score is greater than equal to 4 the imageview should become null. So I tried
putting it outside the button click funtcion but it's not working. It works only when I restart the game e.g I completed level1 with socre >=
4 and when I move back to select level2 it's still locked! When I restart the game then the level is unlocked. Please help me out! Here is
the code

SharedPreferences pref2 = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);


final int score2 = pref2.getInt("score2",0);
if(score2 >= 4)
{
imageview.setImageDrawable(null);
button.getBackground().setAlpha(45);
}

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {

if(score2 >= 4)
{

startActivity(new Intent(Lyricswho.this,Lylevel3.class));
}
else{ Toast.makeText(getApplicationContext(), "Level Locked",
Toast.LENGTH_SHORT).show();}
}
});

android

edited Mar 6 '17 at 3:02 asked Sep 17 '15 at 11:44


Daniel A. White hyeri
155k 40 307 384 84 2 14

1 Answer

From your description it sounds as though you need a thread to be updating after ever score increase. You'll want to call your check on
the score after each score increase wherever that happens in your game code. This check is what is going to remove your blocking
0 image and allow you to proceed.

I believe this is the case because you say that the item works as you intend on reloading the game. Since the preferences are going to
remain there until they are cleared, you are essentially reloading your code post-score increase. What you need to do is just write this
check into your code naturally.

answered Sep 17 '15 at 13:43


Verdant Drift
44 1 1 8

Thanks for the tip ! I think that's is the case. I did some research and the code isn't working :( I will post another question for that – hyeri Sep 20 '15 at
12:42

Você também pode gostar