I have created a height score class by keep track of time. In this class top 5 score are saved in file using Preferences and than they are display on screen.
First I am setting up top5Time array by filling in all zero's. Than I am getting top 5 time from file and fill into top5Time array. Than I am saving in file and displaying the top 5 time on screen.
However there is a bug here. Top5Time gets the data from last run. here is what I mean:
1st time playing - get score of 100 - go to HeighScoreState - update score - display: old data (*This should be 100 not oldData*)
2nd time playing - get score of 40 - go to HeighScoreState - update score - display: score 100 (*This should be 40 not 100*)
3rd time playing - get score of 80 - go to HeighScoreState - update score - display: score 40 (*This should be 80 not 40*)
public class HeighScoreState {
public static Preferences settings = Gdx.app.getPreferences("settingsFile");
public newTime;
public HeighScoreState(int newTime) {
this.newTime = newTime;
// fill top 5 data with '0'
for (int i = 0; i < top5Time.length; i++) {
top5Time[i] = 0;
}
// get top 5 data from file
for (int i = 0; i < top5Time.length; i++) {
top5Time[i] = MyGdxGame.settings.getLong(key[i]);
}
// save data in file
for (int i = 0; i < top5Time.length; i++) {
if (top5Time[i] == 0) {
MyGdxGame.settings.putLong(key[i], newTime);
break;
} else if (newTime <= top5Time[i]) {
MyGdxGame.settings.putLong(key[i], newTime);
break;
}
}
MyGdxGame.settings.flush(); // save data
}
public void render() {
sb.setProjectionMatrix(hudCamera.combined);
sb.begin();
// print data from file
for (int i = 0; i < top5Time.length; i++) {
font.draw(sb,"score: " + top5Time[i], 20, 60 * i);
}
sb.end();
}
}