转自:http://blog.csdn.net/jalen_yuchi/article/details/8617348
先新建一个cocos2dx的工程
HelloWorldScene.cpp中的代码如下:
[cpp]
view plain
copy
print
?
- #include"HelloWorldScene.h"
-
- usingnamespacecocos2d;
- CCScene*HelloWorld::scene()
- {
- CCScene*scene=NULL;
- do
- {
- //'scene'isanautoreleaSEObject
- scene=CCScene::create();
- CC_BREAK_IF(!scene);
-
- //'layer'isanautoreleaSEObject
- HelloWorld*layer=HelloWorld::create();
- CC_BREAK_IF(!layer);
- //addlayerasachildtoscene
- scene->addChild(layer);
- }while(0);
- //returnthescene
- returnscene;
- }
- //on"init"youneedtoinitializeyourinstance
- boolHelloWorld::init()
- boolbRet=false;
- //////////////////////////////////////////////////////////////////////////
- //superinitfirst
- CC_BREAK_IF(!CCLayer::init());
- //addyourcodesbelow...
- //1.Addamenuitemwith"X"image,whichisclickedtoquittheprogram.
- //Createa"close"menuitemwithcloseicon,it'sanautoreleaSEObject.
- CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",85)"> this,153); list-style:decimal-leading-zero outside; color:inherit"> menu_selector(HelloWorld::menuCloseCallback));
- CC_BREAK_IF(!pCloseItem);
- //Placethemenuitembottom-rightconner.
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
- //Createamenuwiththe"close"menuitem,85)"> CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
- pMenu->setPosition(CCPointZero);
- CC_BREAK_IF(!pMenu);
- //AddthemenutoHelloWorldlayerasachildlayer.
- this->addChild(pMenu,1);
- //2.Addalabelshows"HelloWorld".
- //Createalabelandinitializewithstring"HelloWorld".
- //最外面的CCDictionary
- CCDictionary*pDict=CCDictionary::createWithContentsOfFile("strings.xml");
- //第二层CCDictionary
- CCDictionary*pDict_2=newCCDictionary();
- pDict_2->retain();
- pDict_2=(CCDictionary*)pDict->objectForKey("special");
- /*
- 如果在同一个key下面存在<string>hhhhh</string>
- <string>comeontom</string>
- <true></true>
- 这些关键词,则输出最后一个
- */
- 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>
- CCArray*pArray2=newCCArray();
- pArray2->retain();
- pArray2=(CCArray*)pDict_2->objectForKey("special_2");
- for(inti=0;i<pArray2->count();i++)
- 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>
- CCArray*pArray3=newCCArray();
- pArray3->retain();
- pArray3=(CCArray*)pDict_2->objectForKey("special_3");
- inti=0;i<pArray3->count();i++)
- CCLOG("pArray3%i:%s",((CCString*)pArray3->objectAtIndex(i))->getCString());
- /*第三层
- <key>special_4</key>
- <real>多媒体</real>
- CCLOG("pDict_2:%s",((CCString*)pDict_2->valueForKey("special_4"))->getCString());
- <key>special_5</key>
- <false></false>
- "special_5"))->getCString());
- CCLOG("strings.xmlCounts:%d",pDict->count());
- CCLOG("pDict:%s",pDict);
- CCArray*pArray=newCCArray();
- pArray=pDict->allKeys();//把所有的键值付给pArray
- for(inti=0;i<pArray->count();i++)
- CCLOG("pArray%i:%s",((CCString*)pArray->objectAtIndex(i))->getCString());
- }
- 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);
- CC_BREAK_IF(!pLabel);
- //Getwindowsizeandplacethelabelupper.
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- pLabel->setPosition(ccp(size.width/2,size.height-50));
- //AddthelabeltoHelloWorldlayerasachildlayer.
- this->addChild(pLabel,0); background-color:inherit">//3.Addaddasplashscreen,showthecocos2dsplashimage.
- CCSprite*pSprite=CCSprite::create("HelloWorld.png");
- CC_BREAK_IF(!pSprite);
- //Placethespriteonthecenterofthescreen
- pSprite->setPosition(ccp(size.width/2,size.height/2));
- //AddthespritetoHelloWorldlayerasachildlayer.
- this->addChild(pSprite,0);
- bRet=true;
- returnbRet;
- voidHelloWorld::menuCloseCallback(CCObject*pSender)
- //"close"menuitemclicked
- CCDirector::sharedDirector()->end();
- }
strings.xml代码如下:
<?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plistversion="1.0">
- <dict>
- <key>special</key>
- <string>
- <dict>
- <key>special_1</key>
- <string>hhhhh</string>
- <string>comeontom</string>
- <true></true>
- <key>special_2</key>
- <array>
- <string>comeontom1</string>
- <string>comeontom2</string>
- <string>comeontom3</string>
- </array>
- <key>special_3</key>
- <integer>45.0</integer>
- <integer>3</integer>
- <integer>43.444</integer>
- <key>special_4</key>
- <real>多媒体</real>
- <key>special_5</key>
- <false></false>
- </dict>
- </string>
- <key>chinese1</key>
- <string>美好的一天</string>
- <key>japanese</key>
- <string>良い一日を</string>
- <key>spanish</key>
- <string>Buendía</string>
- </plist>
最后来个总结:
dict:建立字典 key:字典里面的关键词 integer:整形,其实和string、real的功能差不多,起配对作用 real:和integer、string的功能差不多,起配对作用 true:<true></true>如果这样写,输出的是1 false:<false></false> 输出的是0 string:和integer、real的功能差不多,起配对作用 array:建立数组 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|