达到XML简单的动态配置

今天写的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 posY,int width){ //加入地板 //加入地板 auto ground = Sprite::create();//就是一个精灵 ground->setPhysicsBody(PhysicsBody::createBox(Size(width,3))); ground->setTextureRect(Rect(0,0,width,3));//设置纹理的 宽 高 ground->setPosition(Vec2(posX,posY));//设置地板的 位置 ground->getPhysicsBody()->setDynamic(false); this->addChild(ground); }



这样就好了,仅仅要你想读取哪一个关卡的信息 就直接在 UserDefault.xml中改动 ID即可了。假设想配置关卡的信息。直接 改动 config.xml文件即可了!感觉这样会略微省事一点,假设哪里不正确请吐槽,一起学习进步,谢谢!

执行界面例如以下:




版权声明:本文博主原创文章。博客,未经同意不得转载。

posted @ 2015-10-01 21:37  hrhguanli  阅读(621)  评论(0编辑  收藏  举报