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


strings.xml代码如下:

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


最后来个总结

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)

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