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

2014/5/24

Leaderboard

1
2
danpost danpost

2014/5/26

#
The next thing would be to have your LeaderboardScreen extract the top 5 scores and display them. Again you should check for availability of storage (that user is logged in and server is available). If not, display a message indicating that the user needs to log in to view leaderboard. Otherwise, loop through the top 5 scoring UserInfo objects using a for-each loop. This all goes in the LeaderboardScreen constructor and would be similar to the following (except for how you display the information):
if (!UserInfo.isStorageAvailable())
{
    System.out.println("You must be logged in to view leaderboard");
}
else
{
    for (Object obj : UserInfo.getTop(5)) // for each of the top 5 scoring users
    {
        UserInfo user = (UserInfo)obj; // the UserInfo object of a top 5 scorer
        String username = user.getUserName(); // the name of the user
        int score = user.getScore(); // the high score of the user
        int rank = user.getRank(); // the rank of the user
        // display information (below is just to terminal for testing)
        System.out.println(""+rank+") "+score+"  "+username);
    }
}
adi110 adi110

2014/5/26

#
danpost wrote...
public void saveScore() { if (UserInfo.isStorageAvailable()) // is user logged in and is server available { UserInfo me = UserInfo.getMyInfo(); // get user info if (getScore() > me.getScore()) // is new score higher than previous score { me.setScore(getScore()); // set new high score me.store(); // save updated info } } }
this goes in world-subclass Sandstone?
adi110 adi110

2014/5/26

#
can't you make a demo for a leaderboard screen saving the best 5 scores?
danpost danpost

2014/5/26

#
adi110 wrote...
can't you make a demo for a leaderboard screen saving the best 5 scores?
See my previous post.
You need to login to post a reply.
1
2