cocos2dx如何读取xml

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

这些天被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. boolbRet=false;
  23. //////////////////////////////////////////////////////////////////////////
  24. //superinitfirst
  25. CC_BREAK_IF(!CCLayer::init());
  26. //addyourcodesbelow...
  27. //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram.
  28. //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject.
  29. CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  30. "CloseNormal.png",
  31. "CloseSelected.png",
  32. this,153); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> menu_selector(HelloWorld::menuCloseCallback));
  33. CC_BREAK_IF(!pCloseItem);
  34. //Placethemenuitembottom-rightconner.
  35. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
  36. //Createamenuwiththe"close"menuitem,it'sanautoreleaSEObject.
  37. CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  38. pMenu->setPosition(CCPointZero);
  39. CC_BREAK_IF(!pMenu);
  40. //AddthemenutoHelloWorldlayerasachildlayer.
  41. this->addChild(pMenu,1);
  42. //2.Addalabelshows"HelloWorld".
  43. //Createalabelandinitializewithstring"HelloWorld".
  44. //最外面的CCDictionary
  45. CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");
  46. //第二层CCDictionary
  47. CCDictionary*pDict_2=newCCDictionary();
  48. pDict_2->retain();
  49. pDict_2=(CCDictionary*)pDict->objectForKey("special");
  50. /*
  51. 如果在同一个key下面存在<string>hhhhh</string>
  52. <string>comeontom</string>
  53. <true></true>
  54. 这些关键词,则输出最后一个
  55. */
  56. CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_1"))->getCString());
  57. /*第三层下面说的是Array中包含string
  58. <key>special_2</key>
  59. <array>
  60. <string>comeontom1</string>
  61. <string>comeontom2</string>
  62. <string>comeontom3</string>
  63. </array>
  64. CCArray*pArray2=newCCArray();
  65. pArray2->retain();
  66. pArray2=(CCArray*)pDict_2->objectForKey("special_2");
  67. for(inti=0;i<pArray2->count();i++)
  68. CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());
  69. <key>special_3</key>
  70. <integer>45.0</integer>
  71. <integer>3</integer>
  72. <integer>43.444</integer>
  73. CCArray*pArray3=newCCArray();
  74. pArray3->retain();
  75. pArray3=(CCArray*)pDict_2->objectForKey("special_3");
  76. inti=0;i<pArray3->count();i++)
  77. CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());
  78. /*第三层
  79. <key>special_4</key>
  80. <real>多媒体</real>
  81. */
  82. CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());
  83. /*第三层
  84. <key>special_5</key>
  85. <false></false>
  86. "special_5"))->getCString());
  87. CCLOG("strings.xmlCounts:%d",pDict->count());
  88. CCLOG("pDict:%s",pDict);
  89. CCArray*pArray=newCCArray();
  90. pArray=pDict->allKeys();//把所有的键值付给pArray
  91. for(inti=0;i<pArray->count();i++)
  92. CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());
  93. }
  94. CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24);
  95. /*CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/
  96. //CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld",24);
  97. CC_BREAK_IF(!pLabel);
  98. //Getwindowsizeandplacethelabelupper.
  99. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  100. pLabel->setPosition(ccp(size.width/2,size.height-50));
  101. //AddthelabeltoHelloWorldlayerasachildlayer.
  102. this->addChild(pLabel,0); background-color:inherit">//3.Addaddasplashscreen,showthecocos2dsplashimage.
  103. CCSprite*pSprite=CCSprite::create("HelloWorld.png");
  104. CC_BREAK_IF(!pSprite);
  105. //Placethespriteonthecenterofthescreen
  106. pSprite->setPosition(ccp(size.width/2,size.height/2));
  107. //AddthespritetoHelloWorldlayerasachildlayer.
  108. this->addChild(pSprite,0);
  109. bRet=true;
  110. returnbRet;
  111. voidHelloWorld::menuCloseCallback(CCObject*pSender)
  112. //"close"menuitemclicked
  113. CCDirector::sharedDirector()->end();
  114. }


strings.xml代码如下:

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

原文来自:http://blog.csdn.net/w18767104183/article/details/28608801

原文链接:/cocos2dx/342114.html

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