Simple data storage in cocos2d-x

Source: Internet
Author: User
Tags xml parser dota

There are many ways to store data in Cocos2d-x. Below I will briefly introduce several types of data, some of which are not accurate, and everyone points out that come and learn together. Thank you!


First, we will introduce the userdefault class:

// Use userdefault to store data. userdefault: getinstance ()-> setstringforkey ("testkey", "testvalue "); // store string-type data // read the data written above STD: String val = userdefault: getinstance ()-> getstringforkey ("testkey "); // read valuelog ("% s", Val. c_str ()); /*************************************** * ********************************* // Of course, there is an int float and other types of storage, I won't say much about it. *//************************************* ***********************************/
The following is a simple application: Save the current game status. Here is an example:

Testusedefault (); /*************************************** * ********************************* // click on the screen the genie, move the genie to a new location, close the program, and open the program again, the location after the wizard moves before it is closed *//**************************** **************************************** * *** // listen to the Click Event auto Listen = eventlistenertouchonebyone:: Create (); listen-> ontouchbegan = [=] (touch * t, event * E) {startpos = T-> getlocation (); // obtain the coordinates of the currently clicked position auto sppos = Sp-> getposition (); // determine whether the on-screen genie if (SP-> getboundingbox () is clicked (). containspoint (startpos) {return true; // click} else {return false ;}; listen-> ontouchmoved = [=] (touch * t, event * E) {endpos = T-> getlocation (); SP-> setposition (endpos); // change the location of the genie at any time }; listen-> ontouchended = [=] (touch * t, event * E) {endpos = Sp-> getposition (); // store the location of the current genie. userdefault :: getinstance ()-> setfloatforkey ("posx", endpos. x); userdefault: getinstance ()-> setfloatforkey ("posy", endpos. y); userdefault: getinstance ()-> flush (); // write data immediately}; // event distribution director: getinstance ()-> geteventdispatcher () -> addeventlistenerwithscenegraphpriority (listen, this );
void DataSaveLayer::testUseDefault(){float posx = UserDefault::getInstance()->getFloatForKey("posx");float posy = UserDefault::getInstance()->getFloatForKey("posy");sp->setPosition(Vec2(posx,posy));}

Okay, let's talk about this class userdefault. Below is the XML reading

Let's start with an XML file. Read it as follows (for example)

<Persons> <person id = "1" type = "W"> <Name> Guan Yu </Name> <attack> 120 </attack> </person> <person id = "W"> "2" type = "R"> <Name> Lu Bu </Name> <attack> 160 </attack> </person> <person id = "3" type = "Q "> <Name> Ma jianjie </Name> <attack> 260 </attack> </person> </persons>

/*************************************** * ********************************** Parse the XML file ideas: 1. get the file path, create the parser xmldoc2. get the root node person3. get the child node, then, take the attributes of the subnode *//******************************* **************************************** */void datasavelayer:: testxml () {auto xmlfilename = fileutils: getinstance ()-> fullpathforfilename ("test. XML "); // get the file location log (" % s ", xmlfilename. c_str (); // verify whether tinyxml2: xmldocument xmldoc is correct; // Declare a parser xmldoc. LoadFile (xmlfilename. c_str (); // resolve the file box .. Xmlelement * root = xmldoc. rootelement (); // obtain the root node xmlelement * person = root-> firstchildelement (); // obtain the child node personxmlelement * name = person-> firstchildelement () of the root node (); // get the sub-node (Guan Yu) of the root node PS: log ("% s", name-> gettext ()); xmlelement * attack = person-> firstchildelement ("attack"); log ("% d", atoi (attack-> gettext ())); // retrieve the attribute log ("ID: % d", person-> intattribute ("ID"); // obtain the root node's subnode (person) attribute log ("type: % s", person-> attribute ("type ")); // obtain the second sub-node xmlelement * person2 = person-> nextsiblingelement ("person"); xmlelement * name2 = person2-> firstchildelement (); log ("Name: % s ", name2-> gettext (); xmlelement * attack2 = person2-> firstchildelement (" attack "); log (" attack: % d ", atoi (attack2-> gettext (); log ("ID: % d", person2-> intattribute ("ID"); log ("type: % s ", person2-> attribute ("type "));}


Now, we will introduce how to create XML.

// Create an XML file /********************************** **************************************// * steps for creating an XML file: 1. first create an XML Parser 2. use the parser to create a node as the root node, and use the parser to create a value as the value of the Root Node 3. use the parser to create a node as the child node of the root node, and use the parser to create a value as the value of the child node of the root node .. 4. Set the attributes of the subnode. 5. The same is true .... Create the second... Third .. *//************************************* * *********************************/Tinyxml2:: xmldocument * Doc = new tinyxml2: xmldocument (); xmlelement * root = doc-> newelement ("role"); // create a node doc-> linkendchild (Root ); // Add this subnode as the last child node auto Sf = doc-> newelement ("SF"); // The parser creates a node root-> linkendchild (SF ); // set this subnode as the final root child node Auto Text = doc-> newtext ("majianjie"); // SF-> linkendchild (text) displayed by SF ); // Add the property SF-> setattribute ("attack", 43); SF-> setattribute ("Hujia", 4); SF-> setattribute ("Speed", 305 ); SF-> setattribute ("HP", 495); Auto dabaojian = doc-> newelement ("dabaojian"); root-> linkendchild (dabaojian ); auto text1 = doc-> newtext ("daboajian"); dabaojian-> linkendchild (text1); // Add the property dabaojian to dabaojian-> setattribute ("ADA", 342 ); dabaojian-> setattribute ("ads", 343); dabaojian-> setattribute ("add", 344); // get the Writable Path auto filename = fileutils: getinstance () -> getwritablepath () + "Dota. XML "; // The created XML file is Dota. xmldoc-> SaveFile (filename. c_str (); // execute the CREATE Command


After the creation command is executed, the Dota. xml file is created under the proj. Win32 \ Debug. Win32 Directory, as shown below:




I will introduce the JSON content tomorrow. Thank you!





Simple data storage in cocos2d-x

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.