Today, I will share with you about the storage of games in cocos2dx and what you need to pay attention to. cocos2dx comes with the storage class CCUserDefault. If you need to store a large amount of data, we recommend that you use databases for storage. Now let's take a look at the CCUserDefault API [cpp] Public Member Functions ~ CCUserDefault () bool getBoolForKey (const char * pKey, bool defaultValue = false) Get bool value by key, if the key doesn't exist, a default value will return. int getIntegerForKey (const char * pKey, int defaultValue = 0) Get integer value by key, if the key doesn't exist, a default value will return. float getFloatForKey (const char * pKey, float defaultValue = 0.0f) Get float value by key, if the key do Esn' t exist, a default value will return. double getDoubleForKey (const char * pKey, double defaultValue = 0.0) Get double value by key, if the key doesn't exist, a default value will return. std: string getStringForKey (const char * pKey, const std: string & defaultValue = "") Get string value by key, if the key doesn't exist, a default value will return. void setBoolForKey (const char * pKey, bool value) Set bool value by key. void setIntegerForKey (const char * pKey, int value) Set integer value by key. void setFloatForKey (const char * pKey, float value) Set float value by key. void setDoubleForKey (const char * pKey, double value) Set double value by key. void setStringForKey (const char * pKey, const std: string & value) Set string value by key. void flush () Save content to xml file. static Publ Ic Member Functions static CCUserDefault * sharedUserDefault () static void purgeSharedUserDefault () static const std: string & getXMLFilePath () You can clearly see the CCUserDefault class, key-Value is used for storage. The key is used to index the Value. For example, [cpp] // store and obtain the data CCUserDefault: sharedUserDefault () -> setStringForKey ("name", "baibai"); CCUserDefault: sharedUserDefault ()-> flush (); // if something has been written, submit std: string name = CCUserDefault:: sharedUse RDefault ()-> getStringForKey ("name"); CCLOG ("name: % s", name. c_str (); well, now we can print the name: baibai Note: 1. Remember to submit the written data. CCUserDefault stores the data in UserDefault. in xml, this file is in the Debug. win32 Directory. You can open this file to view the stored data. 2. Keys follow naming rules. Do not give them names at will. They have suffered losses before. I hope you will keep them in mind. The score [I] name for the key is invalid. Let's write another example [cpp] // save for (int I = 0; I <5; ++ I) {CCString * setScore = CCString :: createWithFormat ("a _ % d", I); CCUserDefault: sharedUserDefault ()-> setIntegerForKey (setScore-> getCString (), a [I]);} CCUserDefault :: sharedUserDefault ()-> flush (); // submit // obtain for (int I = 0; I <5; ++ I) {CCString * getScore = CCString :: createWithFormat ("a _ % d", I); int score [I] = CCUserDefault: sharedUserDefault ()-> getIntegerForKey (getScore-> getCString ()); CCLOG ("score [% d]: % d", I, score [I]);} OK, now this data can be used as a ranking table