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();
        }
    }

Recommended Answers

All 2 Replies

At line 35 you update the settings OK, but you do not update the top5Time array. When you print the results you print them from that array, so the printout does not include the latest value.

Could it be that when you're showing the top time from top5Time in render() that you haven't read the new data from the file yet? A part of the code seems missing so it's hard to tell, but when you are setting the high score with HeighScoreState() you are not updating the top5Time array which you are using in render() to show the score.

Perhaps if you changed the file updating part to also update the top5Time...

 // save data in file
for (int i = 0; i < top5Time.length; i++) {
    if (top5Time[i] == 0) {
        MyGdxGame.settings.putLong(key[i], newTime);

        // new
        top5Time[i]=newTime;

        break;
    } else if (newTime <= top5Time[i]) {
        MyGdxGame.settings.putLong(key[i], newTime);

        // new
        top5Time[i]=newTime;

        break;
    }
}

..it would be able to access the new time in the render() method without having to reload the preference file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.