Today, let's take a look at the data storage class in cocos2dX,CCUserDefaultCurrently, the game basically stores user information so that it can be read when it enters the game again. For convenience, sometimes we can use CCUserDefault to store a simple data item such as the number of gold coins, of course, we recommend that you use databases for large data volumes.
Let's take a look at what functions cocos2dX has prepared for us:
SetBoolForKey (key value, value); // you can specify a bool value.
SetStringForKey (key value, value); // you can specify a string value.
SetDoubleForKey (key value, value); // you can specify a double value.
SetFloatForKey (key value, value); // you can specify a float value.
SetIntegerForKey (key value, value); // you can specify an int value.
GetBoolForKey (key value); // read a bool Value
GetStringForKey (key value); // read a string value
GetDoubleForKey (key value); // read a double value
GetFloatForKey (key value); // read a float Value
GetIntegerForKey (key value); // read an int Value
GetBoolForKey (key value, default value); // read a bool Value
GetStringForKey (key value, default value); // read a string value
GetDoubleForKey (key value, default value); // read a double value
GetFloatForKey (key value, default value); // read a float Value
GetIntegerForKey (key value, default value); // read an int Value
Flush (); // save it to a file
There are also not many functions. There are five types: set writing, get retrieval, and obvious hash table structure, Key-Valua, and Key index Value,
Let's write the code first:
CCLabelTTF * ttf = CCLabelTTF: create ("null", "Arial", 32); ttf-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); addChild (ttf, 0,101); if (CCUserDefault: sharedUserDefault ()-> getBoolForKey ("flag", false) {// data std :: string str = CCUserDefault: sharedUserDefault ()-> getStringForKey ("str", "null"); // retrieve stringttf-> setString (str. c_str (); // set the String title to str} else {// No data CCUserDefault: sharedUserDefault ()-> setStringForKey ("str", "now, hava data "); // write string CCUserDefault: sharedUserDefault ()-> setBoolForKey (" flag ", true); // write boolCCUserDefault: sharedUserDefault () -> flush (); // Save the data to the xml file, but now it seems that you can add or not}
Let's take a look at the first running results:
Second run:
Let's go to Debug. win32 in the project to see if there is an additional UserDefault. xml.
Open this file to see:
You are familiar with it. There are two types of data:
Key: str <===> valua: now, hava data
Key: flag <====> valua: true
However, CCUserDefault provides us with plaintext storage, which can be easily modified by others from outside. Therefore, it is best to encrypt the storage when we store xml files. To prevent players from modifying data, we also write a simple encryption and decryption function:
Define two functions in the header file:
void encode(std::string &str);void decode(std::string &str);
To implement them, I have never studied encryption and decryption algorithms, so the simplest one is:
// Encrypt void HelloWorld: encode (std: string & str) {for (int I = 0; I <str. length (); I ++) {int ch = str [I]; ch = ch ^ 1; str [I] = ch ;}// decrypt void HelloWorld :: decode (std: string & str) {for (int I = 0; I <str. length (); I ++) {int ch = str [I]; ch = ch ^ 1; str [I] = ch ;}}
Use it to see the effect first:
(Narrator: why is there no difference? And fooled us ?), Don't worry. Let's look at the XML file:
Can you understand it? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + fingerprint + HKx9S9wLTUvbzytaXBy7ChLCC5/fingerprint "smirk" src = "http://www.bkjia.com/uploads/allimg/140611/0029395062-11.gif">