Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

Table of Contents (19 chapters)
Learning Cocos2d-x Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 a key named bazookaGameHighScore for the integer value and storing in another integer value named highScore, which will contain the current high score scored in the key from previous games. As we have not used this key before for any of the variables...