今天写的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文件即可了!感觉这样会略微省事一点,假设哪里不正确请吐槽,一起学习进步,谢谢!

执行界面例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFqaWFuamll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFqaWFuamll/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

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

最新文章

  1. Spring Enable annotation – writing a custom Enable annotation
  2. jQuery之核心API
  3. 2014年3月份第1周51Aspx源码发布详情
  4. C语言解析日志,存储数据到伯克利DB
  5. 枚举子集的3种方式 -- C++描述
  6. js时间戳格式化成日期格式
  7. 07-MYSQL多表查询
  8. PHP接口的思考
  9. Sonar配置与使用
  10. 数据注解特性--NotMapped
  11. linux常用命令:netstat 命令
  12. JavaScript快速查找节点
  13. js中移除空白节点
  14. js 延时
  15. Python3 小工具-ARP扫描
  16. Hadoop2.x伪分模式部署
  17. selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
  18. python生成器&amp;迭代器
  19. JMeter上传文件 点选form-data依旧失败的解决方法
  20. tput 命令行使用说明

热门文章

  1. Ubuntu 12.04设置打开远程桌面登录1
  2. CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27
  3. [Asp.Net]状态管理(Session、Application、Cache、Cookie 、Viewstate、隐藏域 、查询字符串)
  4. PL/SQL中字符串变量的分割转化
  5. VS2015试验随手记
  6. jQuery延迟加载(懒加载)插件 – jquery.lazyload.js-Web前端(W3Cways.com) - Web前端学习之路
  7. [Python shelve模块Error]bsddb.db.DBPageNotFoundError: (-30986, &#39;DB_PAGE_NOTFOUND: Requested page not found&#39;)
  8. 学习memcached的一个网站
  9. JAVA语言对比C++语言的几个优点和自身的关键特性
  10. 安装 adobe flash player