Game Development Reference
In-Depth Information
Listing 6-48. Loading the Two Cubes' State
void LoadCubes()
{
m_Cube.LoadObjectState("Cube1Data");
m_Cube2.LoadObjectState("Cube2Data");
}
The LoadGameState() function loads the score and health HUD items, as well as the state of the two
cubes, if their state was previously saved. (See Listing 6-49.)
Listing 6-49. Loading the Game State
void LoadGameState()
{
SharedPreferences settings = m_Context.getSharedPreferences("gamestate", 0);
int StatePreviouslySaved = settings.getInt("previouslysaved", 0);
if (StatePreviouslySaved != 0)
{
// Load in previously saved state
m_Score = settings.getInt("score", 0);
m_Health = settings.getInt("health", 100);
LoadCubes();
}
}
The SaveGameState() function saves the values of the score, health, state of the cubes, and saves a
1 to “previouslysaved.” (See Listing 6-50.)
Listing 6-50. Saving the Game State
void SaveGameState()
{
// We need an Editor object to make preference changes.
SharedPreferences settings = m_Context.getSharedPreferences("gamestate", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("score", m_Score);
editor.putInt("health", m_Health);
SaveCubes();
editor.putInt("previouslysaved", 1);
editor.commit();
}
In the onSurfaceCreated() function, we add code to call the function to load the saved game state,
if there exists one.
LoadGameState();
Search WWH ::




Custom Search