cocos2dx读取xml详细解析

前端之家收集整理的这篇文章主要介绍了cocos2dx读取xml详细解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

From: http://www.jb51.cc/article/p-twvxsamc-dk.html

原文地址:http://blog.csdn.net/comeontom/article/details/7933692

这些天被cocos2dx如何读取xml困惑着,现在总结总结,如有错误,欢迎指正!

先新建一个cocos2dx的工程

HelloWorldScene.cpp中的代码如下:

  1. #include"HelloWorldScene.h"
  2. usingnamespacecocos2d;
  3. CCScene*HelloWorld::scene()
  4. {
  5. CCScene*scene=NULL;
  6. do
  7. {
  8. //'scene'isanautoreleaSEObject
  9. scene=CCScene::create();
  10. CC_BREAK_IF(!scene);
  11. //'layer'isanautoreleaSEObject
  12. HelloWorld*layer=HelloWorld::create();
  13. CC_BREAK_IF(!layer);
  14. //addlayerasachildtoscene
  15. scene->addChild(layer);
  16. }while(0);
  17. //returnthescene
  18. returnscene;
  19. }
  20. //on"init"youneedtoinitializeyourinstance
  21. boolHelloWorld::init()
  22. {
  23. boolbRet=false;
  24. do
  25. {
  26. //////////////////////////////////////////////////////////////////////////
  27. //superinitfirst
  28. //////////////////////////////////////////////////////////////////////////
  29. CC_BREAK_IF(!CCLayer::init());
  30. //////////////////////////////////////////////////////////////////////////
  31. //addyourcodesbelow...
  32. //////////////////////////////////////////////////////////////////////////
  33. //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram.
  34. //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject.
  35. CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  36. "CloseNormal.png",
  37. "CloseSelected.png",
  38. this,
  39. menu_selector(HelloWorld::menuCloseCallback));
  40. CC_BREAK_IF(!pCloseItem);
  41. //Placethemenuitembottom-rightconner.
  42. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
  43. //Createamenuwiththe"close"menuitem,it'sanautoreleaSEObject.
  44. CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  45. pMenu->setPosition(CCPointZero);
  46. CC_BREAK_IF(!pMenu);
  47. //AddthemenutoHelloWorldlayerasachildlayer.
  48. this->addChild(pMenu,1);
  49. //2.Addalabelshows"HelloWorld".
  50. //Createalabelandinitializewithstring"HelloWorld".
  51. //最外面的CCDictionary
  52. CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");
  53. //第二层CCDictionary
  54. CCDictionary*pDict_2=newCCDictionary();
  55. pDict_2->retain();
  56. pDict_2=(CCDictionary*)pDict->objectForKey("special");
  57. /*
  58. 如果在同一个key下面存在<string>hhhhh</string>
  59. <string>comeontom</string>
  60. <true></true>
  61. 这些关键词,则输出最后一个
  62. */
  63. CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());
  64. /*第三层下面说的是Array中包含string
  65. <key>special_2</key>
  66. <array>
  67. <string>comeontom1</string>
  68. <string>comeontom2</string>
  69. <string>comeontom3</string>
  70. </array>
  71. */
  72. CCArray*pArray2=newCCArray();
  73. pArray2->retain();
  74. pArray2=(CCArray*)pDict_2->objectForKey("special_2");
  75. for(inti=0;i<pArray2->count();i++)
  76. {
  77. CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());
  78. }
  79. /*第三层下面说的是Array中包含string
  80. <key>special_3</key>
  81. <array>
  82. <integer>45.0</integer>
  83. <integer>3</integer>
  84. <integer>43.444</integer>
  85. </array>
  86. */
  87. CCArray*pArray3=newCCArray();
  88. pArray3->retain();
  89. pArray3=(CCArray*)pDict_2->objectForKey("special_3");
  90. for(inti=0;i<pArray3->count();i++)
  91. {
  92. CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());
  93. }
  94. /*第三层
  95. <key>special_4</key>
  96. <real>多媒体</real>
  97. */
  98. CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());
  99. /*第三层
  100. <key>special_5</key>
  101. <false></false>
  102. */
  103. CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_5"))->getCString());
  104. CCLOG("strings.xmlCounts:%d",pDict->count());
  105. CCLOG("pDict:%s",pDict);
  106. CCArray*pArray=newCCArray();
  107. pArray=pDict->allKeys();//把所有的键值付给pArray
  108. for(inti=0;i<pArray->count();i++)
  109. {
  110. CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());
  111. }
  112. CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24);
  113. /*CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/
  114. //CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld",24);
  115. CC_BREAK_IF(!pLabel);
  116. //Getwindowsizeandplacethelabelupper.
  117. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  118. pLabel->setPosition(ccp(size.width/2,size.height-50));
  119. //AddthelabeltoHelloWorldlayerasachildlayer.
  120. this->addChild(pLabel,1);
  121. //3.Addaddasplashscreen,showthecocos2dsplashimage.
  122. CCSprite*pSprite=CCSprite::create("HelloWorld.png");
  123. CC_BREAK_IF(!pSprite);
  124. //Placethespriteonthecenterofthescreen
  125. pSprite->setPosition(ccp(size.width/2,size.height/2));
  126. //AddthespritetoHelloWorldlayerasachildlayer.
  127. this->addChild(pSprite,0);
  128. bRet=true;
  129. }while(0);
  130. returnbRet;
  131. }
  132. voidHelloWorld::menuCloseCallback(CCObject*pSender)
  133. {
  134. //"close"menuitemclicked
  135. CCDirector::sharedDirector()->end();
  136. }


strings.xml代码如下:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plistversion="1.0">
  4. <dict>
  5. <key>special</key>
  6. <string>
  7. <dict>
  8. <key>special_1</key>
  9. <string>hhhhh</string>
  10. <string>comeontom</string>
  11. <true></true>
  12. <key>special_2</key>
  13. <array>
  14. <string>comeontom1</string>
  15. <string>comeontom2</string>
  16. <string>comeontom3</string>
  17. </array>
  18. <key>special_3</key>
  19. <array>
  20. <integer>45.0</integer>
  21. <integer>3</integer>
  22. <integer>43.444</integer>
  23. </array>
  24. <key>special_4</key>
  25. <real>多媒体</real>
  26. <key>special_5</key>
  27. <false></false>
  28. </dict>
  29. </string>
  30. <key>chinese1</key>
  31. <string>美好的一天</string>
  32. <key>japanese</key>
  33. <string>良い一日を</string>
  34. <key>spanish</key>
  35. <string>Buendía</string>
  36. </dict>
  37. </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/346173.html

猜你在找的Cocos2d-x相关文章