Using xml in Cocos2d-X
XML can be used to expand the markup language. In game development, it is often used to store game information, such as the highest score, game level, and other information, and describe some resources, the first time I used xml, I used CCAnimation to create an animation. When I used the plist file to load the animation, I used the xml file. The plist file is actually an xml file, in the previous blog, using tile map 1 in the Cocos2d-X and using tile map 2 in the Cocos2d-X using the tile map created by the tile map editor will get a tmx format file, the tmx file is also an xml file
Xml file can also solve the problem of Chinese garbled characters, Chinese encoding garbled characters appear in Cocos2d-X because of different encoding methods, usually used in Windows
VC as IDE, and VC uses GTK encoding, Chinese uses UTF-8 encoding, because of the different encoding methods, so in the Cocos2d-X directly use Chinese will appear garbled, xml uses UTF-8 encoding, so the use of xml can be achieved in the Cocos2d-X to display Chinese
In theory, I will not talk about it, but will not understand it, the following through some examples to introduce the application of xml in Cocos2d-X
Program instance 1: Use CCUserDefault to read information in xml
Implementation process:
1. Create an xml file
2. Write the highest score to the xml file.
3. Highest score for reading xml files
Code for creating an xml file:
// Save the highest score of the game to the memory. CCUserDefault: sharedUserDefault ()-> setIntegerForKey ("HighScore", 7000); // write the highest score of the game to the hard disk. CCUserDefault :: sharedUserDefault ()-> flush ();
After compilation is successful, an xml file named UserDefault. xml is generated in the Debug directory.
The code in UserDefault. xml: UserDefault. xml has the highest score.
-
7000
Highest score for reading UserDefault. xml
// Read the highest score of the game. If no score is obtained, 0 int highScore = CCUserDefault: sharedUserDefault ()-> getIntegerForKey ("HighScore", 0) is returned ); // print the highest score CCLog ("highScore = % d", highScore );
Execution result:
Program Example 2: Use an xml file in plist format to save user information and read user information 1
Implementation process:
Create an xml file in plist format. The content in the file is as follows:
Name
Zhang Sanfeng
Age
36
Add code to the program
// Create a dictionary class to read the xml file CCDictionary * dict = CCDictionary: createWithContentsOfFile ("aaa. plist "); // from aaa. plist reads the name information const CCString * name = dict-> valueForKey ("name"); // from aaa. in plist, read the age information const CCString * age = dict-> valueForKey ("age"); // print the two information CCLog ("name is % s, age is % d ", name-> getCString (), age-> intValue ());
Execution result:
Program Example 3: Use an xml file in plist format to save user information and read user information 2
Implementation process:
Create an xml file in plist format. The content in the file is as follows:
Name
Zhang Sanfeng
Age
36
Family
Son
Name
Xxx
Age
6
Daughter
Name
Yyy
Age
3
Add the following code to the program:
// Create a dictionary class to read the xml file CCDictionary * dict = CCDictionary: createWithContentsOfFile ("aaa. plist "); // from aaa. plist reads the name information const CCString * name = dict-> valueForKey ("name"); // from aaa. in plist, read the age information const CCString * age = dict-> valueForKey ("age"); // print the two information CCLog ("name is % s, age is % d ", name-> getCString (), age-> intValue (); // from aaa. CCObject * oFamily = dict-> objectForKey ("family"); CCDictionary * dictFamily = (CCDictionary *) oFamily; // search for son information in the dictionary. CCDictionary * dictSon = (CCDictionary *) dictFamily-> objectForKey ("son "); // obtain the son name = dictSon-> valueForKey ("name"); // obtain the son age = dictSon-> valueForKey ("age "); // print the son information CCLog ("the name of son is % s, the age of son is % d", name-> getCString (), age-> intValue (); // search for daughter information in the dictionary CCDictionary * dictdaughter = (CCDictionary *) dictFamily-> objectForKey ("daughter "); // search for daughter name = dictdaughter-> valueForKey ("name"); // search for daughter age = dictdaughter-> valueForKey ("age "); // print daughter information CCLog ("the name of daughter is % s, the age of daughter is % d", name-> getCString (), age-> intValue ());
Execution result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Crop + crop/crop = "brush: java;"> # include "T40XML_tinyXML.h" CCScene * T40XML_tinyXML: scene () {CCScene * s = CCScene: create (); t40XML_tinyXML * layer = T40XML_tinyXML: create (); s-> addChild (layer); return s;} void T40XML_tinyXML: WalkOver (tinyxml2: XMLElement * node) {// obtain the first subnode of the root node tinyxml2: XMLElement * curNode = node-> FirstChildElement (); // traverse the while (curNode) node in xml) {if (curNode-> FirstChild () {CCLog ("node is % s, value is % s", curNode-> Value (), curNode-> FirstChild () -> Value ();} else {CCLog ("node is % s, value is NULL", curNode-> Value ();} WalkOver (curNode ); curNode = curNode-> NextSiblingElement () ;}} bool T40XML_tinyXML: init () {CCLayer: init (); // create an xml file tinyxml2 :: XMLDocument * doc = new tinyxml2: XMLDocument; // load the xml document doc-> LoadFile ("aaa. plist "); // print all the content in doc tinyxml2: XMLElement * rootElement = doc-> RootElement (); // print aaa. content on the root node in plist CCLog ("rootElemenet value is % s", rootElement-> Value (); // print aaa. information of all nodes in plist: WalkOver (rootElement); delete doc; return true ;}
Execution result: