加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CCDictionary 解析xml总结

发布时间:2020-12-15 22:58:39 所属栏目:百科 来源:网络整理
导读:转自:http://blog.csdn.net/jalen_yuchi/article/details/8617348 先新建一个cocos2dx的工程 HelloWorldScene.cpp中的代码如下: [cpp] view plain copy print ? #include"HelloWorldScene.h" using namespace cocos2d; CCScene*HelloWorld::scene() { CCSc

转自:http://blog.csdn.net/jalen_yuchi/article/details/8617348


先新建一个cocos2dx的工程

HelloWorldScene.cpp中的代码如下:

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


strings.xml代码如下:

[html] ?
    <?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:建立数组

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读