This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2020/6/12

userinfo

Roshan123 Roshan123

2020/6/12

#
how will i show high score of the user using userinfo
danpost danpost

2020/6/12

#
Roshan123 wrote...
how will i show high score of the user using userinfo
When game ends, if storage is available and final score is greater than user score, set new user score, save to storage and display "NEW HIGH SCORE"; otherwise compare current session high score to final score and do similarly. It would be along these lines:
// in MyWorld
private static int sessionHigh;
int score;

public void act()
{
    if (getObjects(Player.class).isEmpty())
    {
        int high = sessionHigh;
        String text = "FINAL SCORE";
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            high = myInfo.getScore();   
            if (sessionHigh > high)
            {
                myInfo.setScore(sessionHigh);
                myInfo.store();
            }
            if (score > high)
            {
                myInfo.setScore(score);
                myInfo.store();
                high = score;
                text = "NEW HIGH SCORE";
            }
        }
        else if (score > high)
        {
            high = sessionHigh = score;
            text = "SESSION HIGH SCORE";
        }
        Greenfoot.setWorld(new GameOver(score, high, text));
    }
}
You need to login to post a reply.