Respect for the original: http://cn.cocos2d-x.org/tutorial/show?id=2418
Objective
How can a game be saved without a game? A data storage class Ccuserdefault is also available in Cocos2d-x, which can be used as a lightweight database. it supports storage of five types of data bool, int, float, double, string.
"Demo Download"
Userdefault Data storage
"Cocos2d-x 3.x"
(1) Remove "CC"
(2) to obtain a singleton: Shareduserdefault () to getinstance ()
(3) Increase data value:
| 1234 |
// voidsetDataForKey(const char* pKey, const Data& value); Data getDataForKey(const char* pKey, constData& defaultValue = Data::Null); // |
(4) Other changes are small.
"Ccuserdefault"
The Ccuserdefault class, like the previous Ccdirector and Simpleaudioengine, uses a singleton pattern.
It is possible to obtain its unique instance object through the Shareduserdefault () function.
Ccuserdefault uses XML storage technology, which is the general key-value pair, which is similar to the map in C + + (key-value). A keyword corresponds to a value. Its implementation of the interface is also relatively simple and practical, through the traditional set (), get () method to access and modify the value.
It supports five types of data: bool, int, float, double, string.
1. Principle
(1) Map-like mapping (key-value). A keyword corresponds to a value and is accessed using set ()/get ().
(2) The data is stored directly in an XML file, and the file name is the name of the program project, such as "Mytest.xml".
(3) When first used, XML does not exist, and Ccuserdefault automatically creates the corresponding XML file.
2. Setting data value Set
Set by the (key-value) method.
| 1234567 |
// void setBoolForKey( constchar* pKey, boolvalue); //设置一个bool值 voidsetIntegerForKey(constchar* pKey, int value); //设置一个int值 voidsetFloatForKey( constchar* pKey, floatvalue); //设置一个float值 voidsetDoubleForKey( constchar* pKey, doublevalue); //设置一个double值 voidsetStringForKey( constchar* pKey, conststd::string& value); //设置一个string值 // |
3. Get the data value get
Gets the data value from the XML file by using the keyword.
If the keyword does not exist, the (key-value) mapping is set automatically and the corresponding default value, DefaultValue, is set.
| 1234567 |
// boolgetBoolForKey( constchar* pKey, booldefaultValue = false); //读取一个bool值 intgetIntegerForKey(constchar* pKey, intdefaultValue = 0); //读取一个int值 float getFloatForKey( constchar* pKey, floatdefaultValue = 0.0); //读取一个float值 doublegetDoubleForKey( constchar* pKey, double defaultValue = 0.0); //读取一个double值 std::string getStringForKey( constchar* pKey, conststd::string& defaultValue = ""); //读取一个string值 // |
4. Save Flush
When set is finished, the data is not saved to the XML file immediately.
So be sure to remember to use flush () to save the data, otherwise it will be lost!
| 123 |
// CCUserDefault::sharedUserDefault()->flush(); // |
5. Other operations
Gets the singleton object, frees the singleton object, gets the XML file path, and determines whether the XML file already exists.
| 123456 |
// staticCCUserDefault* sharedUserDefault(); //获取单例对象 staticvoid purgeSharedUserDefault(); //释放单例对象 conststaticstd::string& getXMLFilePath(); //获取XML路径 staticboolisXMLFileExist(); //XML文件是否已创建 // |
6, the use of skills
(1) Each operation should be played long Ccuserdefault::shareduserdefault (), to obtain a singleton object. Isn't that a lot of trouble? You can shorten the length by macro definition.
| 123 |
// #define UserDefault CCUserDefault::sharedUserDefault() // |
(2) When using, pay special attention to: the difference between the const char* and the const std::string is not the same!
(3) When set is finished, be sure to remember to use flush () to save the data, otherwise it will be lost!
"Code Combat"
The code originates from the Testcpp project.
1. You need to refer to the following header files and namespaces
Because the string type of C + + is to be used.
| 1234 |
// #include <string> usingnamespacestd; // |
2. Macros define functions to get singleton objects
| 123 |
// #define UserDefault CCUserDefault::sharedUserDefault() // |
3, write data storage related operations, and output data values in the console
| 123456789101112131415161718192021222324252627282930313233343536 |
//设置set UserDefault->setBoolForKey( "bool", true); UserDefault->setIntegerForKey("integer", 100); UserDefault->setFloatForKey( "float", 33.33f); UserDefault->setDoubleForKey( "double", 44.44); UserDefault->setStringForKey( "string", "1111111"); //获取get,并输出到控制台 //通过关键字,获取数据 boolb = UserDefault->getBoolForKey("bool"); int i = UserDefault->getIntegerForKey("integer"); floatf = UserDefault->getFloatForKey("float"); doubled = UserDefault->getDoubleForKey("double"); string ret = UserDefault->getStringForKey("string"); //输出到控制台 CCLOG( (b == true)? "bool is true": "bool is false"); CCLOG("integer is %d", i); CCLOG("float is %f", f); CCLOG("double is %f", d); CCLOG("string is %s", ret.c_str()); //输出XML文件路径 if( UserDefault->isXMLFileExist() ) //是否存在 { string path = UserDefault->getXMLFilePath(); CCLOG("XML file is exist!"); CCLOG( "XML file path : %s", path.c_str() ); } else{ CCLOG("XML file is not exist!"); } //保存数据 UserDefault->flush(); // |
4. Operation result
5. Analysis and summary
(1) Do not know if you find the output of the results of a problem? Float I set the 33.33, why the output is 33.330002. It's not supposed to output 33.330000, right?
This is about the accuracy of C + + floating-point numbers, float is a single-precision floating-point number, and double is a dual-precision floating-point number. The effective accuracy of float in the 6~7 bit, more than 7 bits can be distorted, just like the above example. The effective precision of double is 15~16 bit, more than 16 bits will be distorted. The specific principle, self-Baidu bar!
(2) Getxmlfilepath () Gets the path to the XML file as an absolute path. In addition, the path to the XML file is saved in the project's Debug.win32, stating that the XML file is stored in the directory where the application resides.
Cocos2d-x Data 01:userdefault data storage