Storing high scores

One of the best ways to ensure that the player keeps playing the game is to make him/her beat his/her own high score. Cococs2d-x has a very easy way of storing values in the game, which we will use to compare the previous high score with the current score. If the current score is higher than previous score, we will replace the old high score value with the new one.

For this, in the GameOver() function, add the following:

int highScore = CCUserDefault::sharedUserDefault()->getIntegerForKey("bazookaGameHighScore");

CCUserDefault is a singleton that stores all the user-defined defaults. But you need to provide a unique key for each of the variables that you either want to store or would like to recall.

In this case, I am retrieving ...

Get Learning Cocos2d-x Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.