Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Saving game data


When planning your games, you may soon decide you wish to store data related to your application, such as highest score or user preferences. In Cocos2d-x, you can do this by simply accessing the UserDefault singleton.

With UserDefault, you can store integers, floats, doubles, strings, and Boolean with just one simple call per each data type, as follows:

UserDefault::getInstance()->setIntegerForKey("levelsCompleted", _levelsCompleted);
UserDefault::getInstance()->flush();

The other methods are setFloatForKey, setDoubleForKey, setStringForKey, and setBoolForKey. To retrieve data, you use their respective getters.

I'll show you next how to use that in our game.