今天写了一下XML的相关的内容:用在目前的一个小项目上面(关卡类)因为每关的地图不同,所以让他动态读取XML中的内容,这样修改就可以只看XML文件了。
简单思路:首先用UserDefault类写入文件
UserDefault::getInstance()->setStringForKey("ID","2"); std::string value = UserDefault::getInstance()->getStringForKey("ID"); log("UserDefault: ID = %s",value.c_str());
这样的话在 \proj.win32\Debug.win32下面生成的UserDefault.xml 文件中的内容是:
以后只要过关就向这个文件中写入下一关的 ID就可以了,而关卡的信息在下面的 config.xml中, 注意对应的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>
这些数据就是 每关地图的信息,根据需要自己可以配置。
下面我们就根据UserDefault.xml 中的 ID 来 找到 config.xml 中对应的 关卡信息!!!
新建 一个场景吧。。。
.h文件
#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文件:
#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; } //可视区的大小 Size visSize = Director::getInstance()->getVisibleSize(); //显示边框,注释了就不显示了 this->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); UserDefault::getInstance()->setStringForKey("ID","2");//这里执行一次就ok了,生成文件就好了 std::string value = UserDefault::getInstance()->getStringForKey("ID"); log("UserDefault: ID = %s",value.c_str()); //找到文件 auto xmlFileName = FileUtils::getInstance()->fullPathForFilename("config.xml");//得到文件的位置 log("%s",xmlFileName.c_str());//验证是否正确 //创建解析器 tinyxml2::XMLDocument doc; doc.LoadFile(xmlFileName.c_str());//将文件盒解析器。。 //获取根节点 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(); } //获取名字节点 XMLElement *name = Stage->FirstChildElement(); //获取名字节点的内容 auto content = name->GetText(); name = name->NextSiblingElement(); log("%s",content); while (name!=nullptr){ //获取属性节点(位置) auto one = name->Attribute("one"); auto two = name->Attribute("two"); auto three = name->Attribute("three"); log("%s,%s,%s",one,two,three); //画线 float _one = atof(one); float _two = atof(two); float _three = atof(three); addGround(_one,_two,200); //这里的数据自己填写 name = name->NextSiblingElement(); } return true; } void One::addGround(int posX,int width){ //添加地板 //添加地板 auto ground = Sprite::create();//就是一个精灵 ground->setPhysicsBody(PhysicsBody::createBox(Size(width,3))); ground->setTextureRect(Rect(0,width,3));//设置纹理的 宽 高 ground->setPosition(Vec2(posX,posY));//设置地板的 位置 ground->getPhysicsBody()->setDynamic(false); this->addChild(ground); }
这样就好了,只要你想读取哪一个关卡的信息 就直接在UserDefault.xml中修改 ID就行了,如果想配置关卡的信息,直接 修改 config.xml文件就行了!感觉这样会稍微省事一点,如果哪里不对请吐槽,一起学习进步,谢谢!
运行界面如下: