Game Development Reference
In-Depth Information
The SaveStats() function saves the game-related statistics. For your own game, you can modify the
stats in this class as needed. For example, you could add hit points and character levels if you are
creating a role-playing game. If so, you would have to modify the SaveStats() function to save these
new stats. (See Listing 7-52.)
Listing 7-52. Saving the Stats
void SaveStats(String Handle)
{
SharedPreferences settings = m_Context.getSharedPreferences(Handle, 0);
SharedPreferences.Editor editor = settings.edit();
// Health
String HealthHandle = Handle + "Health";
editor.putInt(HealthHandle, m_Health);
// Commit the edits!
editor.commit();
}
The LoadStats() function loads in the game-related stats. (See Listing 7-53.)
Listing 7-53. Loading in the Stats
void LoadStats(String Handle)
{
// Restore preferences
SharedPreferences settings = m_Context.getSharedPreferences(Handle, 0);
// Health
String HealthHandle = Handle + "Health";
m_Health = settings.getInt(HealthHandle, 100);
}
The functions to retrieve and set the game-related statistics in this class are in Listing 7-54. They
consist of functions relating to damage value, health, and kill value.
Listing 7-54. Getting and Setting the Game-Related Statistics
int GetDamageValue(){return m_DamageValue;}
int GetHealth(){return m_Health;}
int GetKillValue(){return m_KillValue;}
void SetDamageValue(int value){m_DamageValue = value;}
void SetHealth(int health){m_Health = health;}
void SetKillValue(int value){m_KillValue = value;}
 
Search WWH ::




Custom Search