So I have two Bitmap fonts that display scores, one for current score and one for high score. Text for current score works fine, but the one for high score isn't incrementing or saving value.
Here's my code of it:
if (manager.score > highScore){
highScore = manager.score;
preferences.putInteger("highScore", highScore);
int getHighScore;
getHighScore = preferences.getInteger("highScore", highScore);
this.highScoreFont.draw(batch, "" + getHighScore, Gdx.graphics.getWidth() - 1070, Gdx.graphics.getHeight() - 650);
preferences.flush();
}
What am I doing wrong?
GameManager class
public void checkCollisions(){
Iterator<Bullet> it = activeBullets.iterator();
Iterator<BlueMonster> blueMonsterIT = this.blueMonsterArray.iterator();
Bullet bullet;
BlueMonster bMonster;
while (it.hasNext()){
bullet = it.next();
while (blueMonsterIT.hasNext()){
bMonster = blueMonsterIT.next();
boolean collision = Intersector.overlaps(bullet.getRect(), bMonster.getRect());
if (collision == true){
score++;
gameScreen.highScore ++;
sound = Gdx.audio.newSound(Gdx.files.internal("eksplozijaZvuk.ogg"));
sound.play();
Hit hit = hitPool.obtain();
hit.init(bullet.getX(), bullet.getY());
gameStage.addActor(hit);
activeHitMarkers.add(hit);
collision = false;
activeBullets.removeValue(bullet, true);
bullet.remove();
bulletPool.free(bullet);
bMonster.remove();
blueMonsterArray.removeValue(bMonster, true);
blueMonsterPool.free(bMonster);
Timer.schedule(new Timer.Task() {
@Override
public void run() {
Iterator<Hit> it = activeHitMarkers.iterator();
while (it.hasNext()){
Hit hit = it.next();
hitPool.free(hit);
hit.remove();
activeHitMarkers.removeValue(hit, true);
}
}
}, 0.1f);
}
}
}
}
PREFERENCE
Preferences preferences = Gdx.app.getPreferences("highScore");
if (manager.score > highScore){
highScore = manager.score;
preferences.putInteger("highScore", highScore);
preferences.flush();
Gdx.app.log("Prefka", "flushano");
int getHighScore;
getHighScore = preferences.getInteger("highScore", highScore);
highScoreFont.draw(batch, "" + getHighScore, Gdx.graphics.getWidth() - 1070, Gdx.graphics.getHeight() - 650);
highScore = getHighScore;
Gdx.app.log("mjau", "mjau");
}
Note that preferences is initialized globaly
You should call
preferences.flush();
directly after
preferences.putInteger("highScore", highScore);
so the final version should looks like
preferences.putInteger("highScore", highScore);
preferences.flush();
int getHighScore;
getHighScore = preferences.getInteger("highScore", highScore);
You should also remove line
gameScreen.highScore ++;
from your GameManager
class - highscore should be only modified in your condition. Do not forget to initialize highscore
with 0 or current preferences'es value!