首页 > 代码库 > 实现XML的简单动态配置
实现XML的简单动态配置
今天写了一下XML的相关的内容:用在目前的一个小项目上面(关卡类)因为每关的地图不同,所以让他动态读取XML中的内容,这样修改就可以只看XML文件了。
简单思路:首先用UserDefault类写入文件
UserDefault::getInstance()->setStringForKey("ID","2"); std::string value = http://www.mamicode.com/UserDefault::getInstance()->getStringForKey("ID");>这样的话在 \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 = http://www.mamicode.com/UserDefault::getInstance()->getStringForKey("ID");>这样就好了,只要你想读取哪一个关卡的信息 就直接在 UserDefault.xml中修改 ID就行了,如果想配置关卡的信息,直接 修改 config.xml文件就行了!感觉这样会稍微省事一点,如果哪里不对请吐槽,一起学习进步,谢谢!
运行界面如下:
实现XML的简单动态配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。