One of the challenges with a multi-level game is keeping the player's information between levels. There are two common approaches. One is to pass the player data (usually as a player object) from one scene to the next via the scene init
methods. (We used this approach in the snake game in Chapter 4, Give a Snake a Snack.) The other is to use a Singleton.
A Singleton is a design pattern for a class of which there is only one instance allowed. Cocos2D is built on a foundation of Singletons
like
CCDirector
, CCSpriteFrameCache
, and so on. Pretty much anything where you reference a "sharedSomething" (for example, [CCDirector sharedManager]
) is a Singleton. By design, there is a maximum of one "living" version of a Singleton class at any given time.
We will be using a Singleton class to handle our game variables currentLevel
, currentLives
, and currentScore
. Let's take a look:
static BRGameHandler *gameHandler = nil; @implementation BRGameHandler...