最近在学cocos,有很多不错的游戏都是用cocos实现的,其实貌似捕鱼达人4月份流水账有3500W,好奇之下,看了看下捕鱼达人里的一些资源,顺便试着学习下plist的用法。
(接下来用到的资源都在捕鱼达人的APK内,直接解压了就能看到。)
我挑了FishActor-Small_hv.plist和它对应的png来看看下。
FishActor-Small_hv.plist内部的格式是这样的
<dict>
<key>frames</key>
<dict>
<key>btn_music.png</key>
<dict>
<key>frame</key>
<string>{{1, 46}, {31, 16}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0, 0}, {31, 16}}</string>
<key>sourceSize</key>
<string>{31, 16}</string>
</dict>
</dict></dict>
结构是这样的:
文件名
frames
图片名
frame
可以通过这样获取objectCCRect PlistUtils::getRectFromPlist(const char* fileName,const char* key1,const char* key2,const char* key3){ CCDictionary* pDictionary=(CCDictionary*)CCDictionary::createWithContentsOfFile(fileName); CCDictionary* content1=(CCDictionary*)pDictionary->objectForKey(key1); CCDictionary* content2=(CCDictionary*)content1->objectForKey(key2); CCString* object=(CCString*)content2->objectForKey(key3); }
这样就能获取到需要的对象。接下来添加一个开始游戏的menu。首先对HelloWorld改造下,先在init里加入背景
CCSprite* background=CCSprite::create("ui_background_normal_hv.png"); background->setAnchorPoint(CCPointZero); background->setPosition(CCPointZero); addChild(background,0);
再加入开始游戏的按钮,开始游戏的贴图是通过,开始游戏的贴图在UI_GameMenuText_cn_hv.png和UI_GameMenuText_cn_hv.plist内首先获取开始游戏的CCRect
CCTexture2D* menusCache=CCTextureCache::sharedTextureCache()->addImage("UI_GameMenuText_cn_hv.png"); CCRect startRect=PlistUtils::getRectFromPlist("UI_GameMenuText_cn_hv.plist","frames","ui_2p_010.png","frame"); CCSprite* startSprite=CCSprite::createWithTexture(menusCache,startRect); CCMenuItemSprite* pStartMenu=CCMenuItemSprite::create(startSprite,startSprite,this,menu_selector(GameMenuScence::menuStartCallback)); pStartMenu->setAnchorPoint(ccp(0.5f,0.5f)); pStartMenu->setPosition(ccp(size.width/2,size.height/2)); CCMenu* pMenus=CCMenu::create(pStartMenu,NULL); pStartMenu->setPosition(CCPointZero); addChild(pMenus,2);