Game Development Reference
In-Depth Information
The compareTo() function is used in combination with the Collections.sort() function to sort high
score table entries in descending order, with high scores ordered first (see Listing 9-10). Normally,
the compareTo() function sorts the entries in ascending order. Because we are sorting in descending
order, we need to make some key changes.
The function returns a negative integer if the score in this instance of the entry is
greater than the score in the input parameter variable Another 's score.
The function returns a positive integer if the score in this instance of the entry is
less than the score in the input parameter variable Another 's score.
Listing 9-10. Comparing and Sorting Entries
public int compareTo(HighScoreEntry Another)
{
/*
Normally ascending sorting - Returns
a negative integer if this instance is less than another; a positive integer if this
instance is greater than another; 0 if this instance has the same order as another.
*/
int result = 0;
if (m_Score > Another.m_Score)
{
result = -1;
}
else
if (m_Score < Another.m_Score)
{
result = 1;
}
return result;
}
The HighScoreTable Class
The HighScoreTable class represents the high score table containing all the player's high
score entries.
The HIGH_SCORES string variable holds the handle for loading and saving the high score table.
private String HIGH_SCORES = "HighScores";
The MAX_RANK variable holds the maximum number of high scores to display.
private int MAX_RANK = 10;
The MAX_SCORES variable holds the maximum number of scores that are stored internally for
processing and calculation purposes.
private int MAX_SCORES = 11;
 
Search WWH ::




Custom Search