Cocos2dX Data Storage

Source: Internet
Author: User

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">

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.