0
\$\begingroup\$

How do I make it so that in a multiple-level game the score will stay where it was from the previous level?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .