转自:http://blog.csdn.net/jalen_yuchi/article/details/8617348
先新建一个cocos2dx的工程
HelloWorldScene.cpp中的代码如下:
- @H_502_37@#include"HelloWorldScene.h"
- @H_502_37@
- @H_502_37@usingnamespacecocos2d;
- @H_502_37@CCScene*HelloWorld::scene()
- @H_502_37@{
- @H_502_37@CCScene*scene=NULL;
- @H_502_37@do
- @H_502_37@{
- @H_502_37@//'scene'isanautoreleaSEObject
- @H_502_37@scene=CCScene::create();
- @H_502_37@CC_BREAK_IF(!scene);
- @H_502_37@
- //'layer'isanautoreleaSEObject
- @H_502_37@HelloWorld*layer=HelloWorld::create();
- @H_502_37@CC_BREAK_IF(!layer);
- //addlayerasachildtoscene
- @H_502_37@scene->addChild(layer);
- @H_502_37@}while(0);
- //returnthescene
- @H_502_37@returnscene;
- @H_502_37@}
- //on"init"youneedtoinitializeyourinstance
- @H_502_37@boolHelloWorld::init()
- boolbRet=false;
- //////////////////////////////////////////////////////////////////////////
- @H_502_37@//superinitfirst
- @H_502_37@CC_BREAK_IF(!CCLayer::init());
- //addyourcodesbelow...
- //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram.
- //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject.
- @H_502_37@CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
- @H_502_37@"CloseNormal.png",
- @H_502_37@"CloseSelected.png",85)"> @H_502_37@this,153); list-style:decimal-leading-zero outside; color:inherit"> @H_502_37@menu_selector(HelloWorld::menuCloseCallback));
- @H_502_37@CC_BREAK_IF(!pCloseItem);
- //Placethemenuitembottom-rightconner.
- @H_502_37@pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
- //Createamenuwiththe"close"menuitem,85)"> @H_502_37@CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
- @H_502_37@pMenu->setPosition(CCPointZero);
- @H_502_37@CC_BREAK_IF(!pMenu);
- //AddthemenutoHelloWorldlayerasachildlayer.
- @H_502_37@this->addChild(pMenu,1);
- //2.Addalabelshows"HelloWorld".
- //Createalabelandinitializewithstring"HelloWorld".
- //最外面的CCDictionary
- @H_502_37@CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");
- //第二层CCDictionary
- @H_502_37@CCDictionary*pDict_2=newCCDictionary();
- @H_502_37@pDict_2->retain();
- @H_502_37@pDict_2=(CCDictionary*)pDict->objectForKey("special");
- /*
- 如果在同一个key下面存在<string>hhhhh</string>
- <string>comeontom</string>
- <true></true>
- 这些关键词,则输出最后一个
- */
- @H_502_37@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>
- @H_502_37@CCArray*pArray2=newCCArray();
- @H_502_37@pArray2->retain();
- @H_502_37@pArray2=(CCArray*)pDict_2->objectForKey("special_2");
- @H_502_37@for(inti=0;i<pArray2->count();i++)
- @H_502_37@CCLOG("pArray2%i:%s",i+1,((CCString*)pArray2->objectAtIndex(i))->getCString());
- <key>special_3</key>
- <integer>45.0</integer>
- <integer>3</integer>
- <integer>43.444</integer>
- @H_502_37@CCArray*pArray3=newCCArray();
- @H_502_37@pArray3->retain();
- @H_502_37@pArray3=(CCArray*)pDict_2->objectForKey("special_3");
- inti=0;i<pArray3->count();i++)
- @H_502_37@CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());
- /*第三层
- <key>special_4</key>
- <real>多媒体</real>
- @H_502_37@CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());
- <key>special_5</key>
- <false></false>
- "special_5"))->getCString());
- @H_502_37@CCLOG("strings.xmlCounts:%d",pDict->count());
- @H_502_37@CCLOG("pDict:%s",pDict);
- @H_502_37@CCArray*pArray=newCCArray();
- @H_502_37@pArray=pDict->allKeys();//把所有的键值付给pArray
- @H_502_37@for(inti=0;i<pArray->count();i++)
- @H_502_37@CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());
- @H_502_37@}
- @H_502_37@CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pDict->objectForKey("chinese1"))->getCString(),"Arial",24);
- /*CCLabelTTF*pLabel=CCLabelTTF::create(((CCString*)pArray->objectAtIndex(3))->getCString(),"Arial",24);*/
- //CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld",24);
- @H_502_37@CC_BREAK_IF(!pLabel);
- //Getwindowsizeandplacethelabelupper.
- @H_502_37@CCSizesize=CCDirector::sharedDirector()->getWinSize();
- @H_502_37@pLabel->setPosition(ccp(size.width/2,size.height-50));
- //AddthelabeltoHelloWorldlayerasachildlayer.
- @H_502_37@this->addChild(pLabel,0); background-color:inherit">//3.Addaddasplashscreen,showthecocos2dsplashimage.
- @H_502_37@CCSprite*pSprite=CCSprite::create("HelloWorld.png");
- @H_502_37@CC_BREAK_IF(!pSprite);
- //Placethespriteonthecenterofthescreen
- @H_502_37@pSprite->setPosition(ccp(size.width/2,size.height/2));
- //AddthespritetoHelloWorldlayerasachildlayer.
- @H_502_37@this->addChild(pSprite,0);
- @H_502_37@bRet=true;
- @H_502_37@returnbRet;
- @H_502_37@voidHelloWorld::menuCloseCallback(CCObject*pSender)
- //"close"menuitemclicked
- @H_502_37@CCDirector::sharedDirector()->end();
- @H_502_37@}
strings.xml代码如下: