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.