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 CCUserDefault
singleton.
With CCUserDefault
you can store integers
, floats
, doubles
, strings
, and boolean
with just one simple call per data type. For example:
CCUserDefault::sharedUserDefault()->setIntegerForKey(levelsCompleted, _levelsCompleted);
CCUserDefault::sharedUserDefault()->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.