Advertisement

SharedPreferences in my game

Started by January 16, 2016 04:52 PM
0 comments, last by imtang 9 years, 1 month ago

Hello gamedev Community, I have a problem with my sharedPrefs and I dont get it (im a beginner) I want to make a simple game but i stuck at the HiScore.It allways displays O. Those are some code snippets.

MainActivity:
SharedPreferences prefs;
SharedPreferences.Editor editor;
prefs = getSharedPreferences("HiScores", MODE_PRIVATE);
long longestDistance = prefs.getLong("longestDistance", 1000000);

GameActivity:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
prefs = context.getSharedPreferences("HiScores", context.MODE_PRIVATE);
editor = prefs.edit();
longestDistance = prefs.getLong("longestDistance", 1000000);

private void update() {
if(distanceTaken < longestDistance) {
editor.putLong("longestDistance", distanceTaken);
editor.commit();
longestDistance = distanceTaken;
}
if(distanceTaken >= 100000) {
distanceTaken = 100000;
gameEnded = true;
}
private void startGame(){
distanceTaken = 0;
}

Thank you for the help :) Niclas

I found the mistake :)

This topic is closed to new replies.

Advertisement