Today, I wrote something about XML: I used it on a small project (the card-off class). Because the map on each level is different, I asked him to dynamically read the content in XML, in this way, you can only view the XML file.
Simple idea: first, use the userdefault class to write files.
UserDefault::getInstance()->setStringForKey("ID","2");std::string value = UserDefault::getInstance()->getStringForKey("ID");log("UserDefault: ID = %s",value.c_str());
In this case, the userdefault. xml file generated under \ proj. Win32 \ Debug. Win32 is as follows:
In the future, you only need to write the ID of the next level to this file, and the information of the level is in the config. xml below,Note the corresponding ID
Config. xml:
<Root><Stage ID = "1"><name>map01.tmx</name><property1 one = "256" two = "168" three = "visSize.width/4" /><property2 one = "480" two = "168" three = "visSize.width/17.1" /><property3 one = "704" two = "168" three = "visSize.width/4" /><property4 one = "144" two = "294" three = "visSize.width/3.5" /><property5 one = "480" two = "294" three = "visSize.width/5.5" /><property6 one = "816" two = "296" three = "visSize.width/3.4" /><property7 one = "320" two = "424" three = "visSize.width/8" /><property8 one = "672" two = "424" three = "visSize.width/8" /></Stage><Stage ID = "2"><name>map02.tmx</name><property1 one = "256" two = "168" three = "visSize.width/4" /><property2 one = "480" two = "168" three = "visSize.width/17.1" /><property3 one = "704" two = "168" three = "visSize.width/4" /><property4 one = "144" two = "294" three = "visSize.width/3.5" /><property5 one = "480" two = "294" three = "visSize.width/5.5" /><property5 one = "816" two = "296" three = "visSize.width/3.4" /><property5 one = "320" two = "424" three = "visSize.width/8" /><property5 one = "672" two = "424" three = "visSize.width/8" /></Stage><Stage ID = "3"><name>map01.tmx</name><property4 one = "144" two = "294" three = "visSize.width/3.5" /><property5 one = "480" two = "294" three = "visSize.width/5.5" /><property6 one = "816" two = "296" three = "visSize.width/3.4" /><property7 one = "320" two = "424" three = "visSize.width/8" /><property8 one = "672" two = "424" three = "visSize.width/8" /></Stage><Stage ID = "4"><name>map02.tmx</name><property1 one = "256" two = "168" three = "visSize.width/4" /><property2 one = "480" two = "168" three = "visSize.width/17.1" /><property3 one = "704" two = "168" three = "visSize.width/4" /></Stage> </Root>
The data is the information of each map, which can be configured as needed.
Next we will find the corresponding level information in config. XML based on the ID in userdefault. xml !!!
Create a new scenario...
. H file
#pragma once#include "cocos2d.h"#include "tinyxml2/tinyxml2.h"using namespace cocos2d;using namespace tinyxml2;class One:public Scene{public:virtual bool init();CREATE_FUNC(One);static Scene *createScene();XMLElement *Stage;std::string ID;void addGround(int posX,int posY,int width);};
. Cpp file:
# Include "one. H "# include" Two. H "scene * One: createscene () {SCENE * scene = scene: Create (); Auto layer = one: Create (); scene-> addchild (layer ); return scene;} bool one: Init () {If (! Scene: initwithphysics () {return false;} // visible area size vissize = Director: getinstance ()-> getvisiblesize (); // display the border, this-> getphysicsworld ()-> setdebugdrawmask (physicsworld: debugdraw_all); userdefault: getinstance ()-> setstringforkey ("ID", "2 "); // The execution is OK once, and the file is generated. STD: string value = userdefault: getinstance ()-> getstringforkey ("ID"); log ("userdefault: id = % s ", value. c_str (); // find the file auto xmlfilenam E = fileutils: getinstance ()-> fullpathforfilename ("config. XML "); // get the file location log (" % s ", xmlfilename. c_str (); // verify whether the parser is correct // create the parser tinyxml2: xmldocument Doc; Doc. loadFile (xmlfilename. c_str (); // parses the file box .. // Obtain the root node xmlelement * root = Doc. rootelement (); // xmlelement * stage = root-> firstchildelement (); While (stage! = Nullptr) {Auto ID = stage-> attribute ("ID"); If (ID = value) {id = ID; Stage = stage ;} stage = stage-> nextsiblingelement ();} // obtain the Name node xmlelement * name = stage-> firstchildelement (); // get the content of the Name node auto content = Name-> gettext (); name = Name-> nextsiblingelement (); log ("% s", content); While (name! = Nullptr) {// obtain the attribute node (location) Auto one = Name-> attribute ("one"); Auto two = Name-> attribute ("two "); auto Three = Name-> attribute ("three"); log ("% s, % s, % s", one, two, three ); // draw line float _ One = atof (one); float _ Two = atof (two); float _ Three = atof (three); addground (_ One, _ two, 200); // enter name = Name-> nextsiblingelement ();} return true;} void one: addground (INT posx, int Posy, int width) {// Add floor auto ground = sprite: Create (); // It is a genie ground-> setphysicsbody (physicsbody: createbox (SIZE (width, (3); ground-> settexturerect (rect (, width, 3); // set the width and height of the texture ground-> setposition (vec2 (posx, Posy )); // set the floor position to ground-> getphysicsbody ()-> setdynamic (false); this-> addchild (ground );}
In this way, as long as you want to read the information of which level, it is directly in userdefault. modify the ID in XML. If you want to configure the level information, directly modify config. the XML file is enough! It will save you a little trouble. If there is something wrong, please speak out and study and make progress together. Thank you!
The running interface is as follows:
Simple and dynamic configuration of XML