大佬教程收集整理的这篇文章主要介绍了cocos2dx读取xml详细解析,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
From: http://www.voidcn.com/article/p-twvxsamc-dk.html
原文地址:http://blog.csdn.net/comeontom/article/details/7933692】
这些天被cocos2dx如何读取xml困惑着,现在总结总结,如有错误,欢迎指正!
先新建一个cocos2dx的工程
HelloWorldScene.cpp中的代码如下:
#include "HelloWorldScene.h" using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scenE); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image,which is clicked to quit the program. // Create a "close" menu item with close icon,it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png","CloseSELEcted.png",this,menu_SELEctor(HelloWorld::menuCloseCallBACk)); CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20,20)); // Create a menu with the "close" menu item,it's an auto release object. CCMenu* pMenu = CCMenu::create(pCloseItem,null); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer. this->addChild(pMenu,1); // 2. Add a label shows "Hello World". // Create a label and initialize with String "Hello World". //最外面的 CCDictionary CCDictionary* pDict = CCDictionary::createWithContentsOfFile("Strings.xml"); //第二层 CCDictionary CCDictionary* pDict_2 = new CCDictionary(); pDict_2->retain(); pDict_2 = (CCDictionary*)pDict->objectForKey("special"); /* 如果在同一个key下面存在<String>hhhhh</String> <String>comeontom</String> <true></true> 这些关键词,则输出最后一个 */ CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString()); /*第三层 下面说的是Array中包含String <key>special_2</key> <array> <String>comeontom1</String> <String>comeontom2</String> <String>comeontom3</String> </array> */ CCArray* pArray2 = new CCArray(); pArray2->retain(); pArray2 = (CCArray*)pDict_2->objectForKey("special_2"); for (int i = 0;i < pArray2->count();i++) { CCLOG("pArray2%i:%s",((CCString*)pArray2->objectATindex(i))->getCString()); } /*第三层 下面说的是Array中包含String <key>special_3</key> <array> <Integer>45.0</Integer> <Integer>3</Integer> <Integer>43.444</Integer> </array> */ CCArray* pArray3 = new CCArray(); pArray3->retain(); pArray3 = (CCArray*)pDict_2->objectForKey("special_3"); for (int i = 0;i < pArray3->count();i++) { CCLOG("pArray3%i:%s",((CCString*)pArray3->objectATindex(i))->getCString()); } /*第三层 <key>special_4</key> <real>多媒体</real> */ CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString()); /*第三层 <key>special_5</key> <false></false> */ CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString()); CCLOG("Strings.xmlCounts:%d",pDict->count()); CCLOG("pDict:%s",pDict); CCArray* pArray = new CCArray(); pArray = pDict->allKeys();//把所有的键值付给pArray for (int i = 0;i < pArray->count();i++) { CCLOG("pArray%i:%s",((CCString*)pArray->objectATindex(i))->getCString()); } CCLabelTTF* pLabel = CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),24); /*CCLabelTTF* pLabel = CCLabelTTF::create(((CCString*)pArray->objectATindex(3))->getCString(),24);*/ // CCLabelTTF* pLabel = CCLabelTTF::create("Hello World",24); CC_BREAK_IF(! pLabel); // Get window size and place the label upper. CCSize size = CCDirector::sharedDirector()->getWinSize(); pLabel->setPosition(ccp(size.width / 2,size.height - 50)); // Add the label to HelloWorld layer as a child layer. this->addChild(pLabel,1); // 3. Add add a splash screen,show the cocos2d splash image. CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CC_BREAK_IF(! pSpritE); // Place the sprite on the center of the screen pSprite->setPosition(ccp(size.width/2,size.height/2)); // Add the sprite to HelloWorld layer as a child layer. this->addChild(pSprite,0); bRet = true; } while (0); return bRet; } void HelloWorld::menuCloseCallBACk(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <Dict> <key>special</key> <String> <Dict> <key>special_1</key> <String>hhhhh</String> <String>comeontom</String> <true></true> <key>special_2</key> <array> <String>comeontom1</String> <String>comeontom2</String> <String>comeontom3</String> </array> <key>special_3</key> <array> <Integer>45.0</Integer> <Integer>3</Integer> <Integer>43.444</Integer> </array> <key>special_4</key> <real>多媒体</real> <key>special_5</key> <false></false> </Dict> </String> <key>chinese1</key> <String>美好的一天</String> <key>japanese</key> <String>良い一日を</String> <key>spanish</key> <String>Buen día</String> </Dict> </plist>
最后来个总结:
Dict:建立字典
key:字典里面的关键词
Integer:整形,其实和String、real的功能差不多,起配对作用
real:和Integer、String的功能差不多,起配对作用
true:<true></true>如果这样写,输出的是1
false:<false></false> 输出的是0
String:和Integer、real的功能差不多,起配对作用
array:建立数组
当然了,有兴趣看源码的同学,可以看这个文件:CCFileUtilsCommon_cpp.h(位置:cocos2dx\platform)
以上是大佬教程为你收集整理的cocos2dx读取xml详细解析全部内容,希望文章能够帮你解决cocos2dx读取xml详细解析所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。