• cocos2d-x 在XML分析和数据存储


    无意中起到一周中的游戏,哎,时间过得总是打得那么快时,。

    。。

    可怜

    于是今天决定看一下之前不怎么非常熟悉的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 
    



    //.cpp文件

    #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
    }
    

    上面分别包括了xml的解析即xml的数据显示:

    显演示样例如以下:

    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>

    控制节目。输出和以前创建xml一致性,就这样,xml在分辨率ok 该,它不是很easy啊微笑

  • 相关阅读:
    XStream教程
    Log4j教程
    Java.io包
    Java输入/输出教程
    Java.math.BigDecimal.abs()方法
    数据类型转换
    JUnit教程
    java.lang
    标识符
    PHP面向对象笔记解析
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4591857.html
Copyright © 2020-2023  润新知