How do I make it so that in a multiple-level game the score will stay where it was from the previous level?
1 Answer
Store the score outside of the level, probably in the game object that manages which level you are on:
class Game {
int score;
Level * currentLevel;
...
};
If there is code within a given level object that might need to manipulate the score, you can pass that information to the level when it is constructed. You could simply pass a pointer to the score integer (the simplest, most straightforward option) or you could encapsulate the score in its own interface:
struct Score {
int GetCurrentPoints () const;
void AddPoints (int points);
...
};
struct Level {
Level (Score * globalScore)
: score(globalScore) {
}
private:
Score * score;
};
class Game {
Score score;
Level * currentLevel;
...
};
Pass the a pointer to the game's instance of the single global score to each level, which they store and manipulate, and it will persist across levels as you'd like.