标签:显示 ccscene share get stat white tor cto new t
一不小心就玩了一周的游戏了。哎。玩的时候时间过得总是这么快。。。
于是今天决定看一下之前不怎么非常熟悉的XML;(之前做游戏时数据的储存用到过XML,但这块是还有一个同事在做,所以不怎么熟悉),
看了看他写的xml和解析方法。然后自己改进了下。所以来简单的总结和分享分享
主要涉及到的有:
1. xml 创建
2.xml的解析
3.将解析后的xml数据用vector保存起来。
例如以下:(写完xml后,最简单的检查语法错误就是用IE浏览器打开看看,能够打开则说明语法没错)
<?xml version="1.0" encoding="utf-8"?> <Mineral> <mineral> <type>1</type> <times>100</times> <p>20</p> </mineral> <mineral> <type>4</type> <times>100</times> <p>20</p> </mineral> <mineral> <type>5</type> <times>100</times> <p>20</p> </mineral> </Mineral>在这里我依照网上的XML书写格式新建了一个名为 "Mineral.xml"的xml;
(Mineral就是矿的意思,xml 中我任意写了3中类型的矿石,每种矿石有自己的类型、倍率、概率)
然后将其保存在资源目录里面,然后新建一个cocos2d-x项目。
以下贴出主要解析代码
//.h文件
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include <string> #include <vector> typedef struct Mineral { int times ; int type; int p; }*PtrMineral; class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); void menuCloseCallback(CCObject* pSender); bool readMinearlXml(); void displayVec(); CREATE_FUNC(HelloWorld); private: std::vector<PtrMineral >m_pMineralVec ; }; #endif
#include "HelloWorldScene.h" #include "../support/tinyxml2/tinyxml2.h" using namespace tinyxml2; USING_NS_CC; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } readMinearlXml(); displayVec(); return true; } bool HelloWorld::readMinearlXml() { tinyxml2::XMLDocument* xmlData = new tinyxml2::XMLDocument(); unsigned long nSize ; const char *pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData("XML/Mineral.xml","rb",&nSize); if( NULL == pXmlBuffer ) { CCLOG("read Mineral.xml Failed"); } else CCLOG("star read Mineral.xml"); xmlData->Parse(pXmlBuffer,nSize); XMLElement *rootNode = xmlData->RootElement(); if(!rootNode) { return false; } XMLElement* curNode = rootNode->FirstChildElement("mineral"); while(NULL!= curNode) { PtrMineral pMineral =new Mineral(); pMineral->type = (atoi)( (curNode->FirstChildElement("type"))->GetText() ); pMineral->times = (atoi)( (curNode->FirstChildElement("times"))->GetText() ); pMineral->p = (atoi)( (curNode->FirstChildElement("p"))->GetText() ); m_pMineralVec.push_back(pMineral); curNode = curNode->NextSiblingElement("mineral"); } delete xmlData; return true; } void HelloWorld::displayVec() { CCLOG("*********m_pMineralVec*********"); for(int i = 0 ; i<m_pMineralVec.size() ; i++) { CCLOG("<mineral>"); CCLOG(" <type> = %i </type>",m_pMineralVec[i]->type); CCLOG(" <times> = %i </times>",m_pMineralVec[i]->times); CCLOG(" <p> = %i </p>",m_pMineralVec[i]->p); CCLOG("</mineral>"); } } void HelloWorld::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif #endif }
显演示样例如以下:
star read Mineral.xml *********m_pMineralVec********* <mineral> <type> = 1 </type> <times> = 100 </times> <p> = 20 </p> </mineral> <mineral> <type> = 4 </type> <times> = 100 </times> <p> = 20 </p> </mineral> <mineral> <type> = 5 </type> <times> = 100 </times> <p> = 20 </p> </mineral>
标签:显示 ccscene share get stat white tor cto new t
原文地址:http://www.cnblogs.com/brucemengbm/p/6919877.html