尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17144051
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人7】游戏场景基础实现
1、TinyXml与配置文件
XML
一种可扩展性标记语言
XML示例
<node attribute="value">
<sub_node>value</sub_node>
<sub_node_r attribute="value" />
</node>
Fishing Joy可以用到XML配置文件的地方
UI Layout
场景配置
音乐配置
鱼动画配置
2、鱼的配置文件
鱼动画特点
两种动画:移动和死亡
移动动画和死亡动画帧率相同
需要配置的信息
鱼动画名
移动动画文件名,帧数
死亡动画文件名,帧数
先看下鱼的配置信息:
<fishs> <!--clownfish--> <fish template_id="1000"> <move file="clownfish_1.png" fps="5"/> <atk file="clownfish_2.png" fps="5"/> </fish> <!--shark--> <fish template_id="1001"> <move file="shark_1.png" fps="4"/> <atk file="shark_2.png" fps="4"/> </fish> </fishs>游戏场景的配置信息为:
<game> <begin>media/begin.png</begin> <play>media/play.png</play> </game>
来看具体的代码实现:
bool CGameLogic::init( CCScene* pScene ) { //获得TemplateID为1000的鱼的配置 config::CFishConfig::getData(1000); m_eGameState = EGS_BEGIN_MENU; if(pScene==NULL) return false; m_pGameScene = scene::createScene(); if(m_pGameScene==NULL) return false; cocos2d::CCLayer* pSceneRoot = cocos2d::CCLayer::create(); pScene->addChild(pSceneRoot); m_pGameScene->attachTo(pSceneRoot); //获取BeginScene的背景图片 const char* szBeginSceneBg = config::CGameConfig::getBeginSceneBg(); m_pGameScene->setBackground(szBeginSceneBg); cocos2d::CCLayer* pUIRoot = cocos2d::CCLayer::create(); pScene->addChild(pUIRoot); mUIMgr.init(this,pUIRoot); return true; }接着看鱼配置信息的解析
#ifndef __FISH_CONFIG_H__ #define __FISH_CONFIG_H__ #include "../tinyxml/tinyxml.h" #include "cocos2d.h" #include <string> #include <map> namespace fishingjoy { namespace config { struct FishData { int TemplateID; std::string MoveFile; int MoveFps; std::string AtkFile; int AtkFps; }; class CFishConfig { static std::map<int,FishData> m_mapFishData; static void load() { if(m_mapFishData.size() ==0){ TiXmlDocument doc; const char* pFile = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("config/fishs.xml"); if(doc.LoadFile(pFile)){ TiXmlElement* root = doc.RootElement(); TiXmlElement* fish = root->FirstChildElement(); while(fish){ FishData data; data.TemplateID = atoi(fish->Attribute("template_id")); TiXmlElement* node = fish->FirstChildElement(); data.MoveFile = node->Attribute("file"); data.MoveFps = atoi(node->Attribute("fps")); node = node->NextSiblingElement(); data.AtkFile = node->Attribute("file"); data.AtkFps = atoi(node->Attribute("fps")); //将FishData存入map里面 m_mapFishData[data.TemplateID] = data; fish = fish->NextSiblingElement(); } } } } public: static const FishData* getData(int tid) { //调用load()函数初始化资源 load(); std::map<int,FishData>::iterator it = m_mapFishData.find(tid); if(it!=m_mapFishData.end()){ return &it->second; } return NULL; } }; }; } #endif点击开始游戏按钮之后进入游戏:
#ifndef _GAME_LOGIC_H_ #define _GAME_LOGIC_H_ #include "cocos2d.h" USING_NS_CC; #include <vector> #include "../include/IGameScene.h" #include "../include/SignalSlot.h" #include "../config/GameConfig.h" #include "../config/FishConfig.h" #include "GameLogicDef.h" #include "GameUIMgr.h" #include "GameObject.h" namespace fishingjoy { namespace logic { class CGameLogic:public cocos2d::CCObject,public IGameLogic { scene::IGameScene* m_pGameScene; CGameUIMgr mUIMgr; std::vector<CGameObject*> m_vecObjectsList; EGameState m_eGameState; public: //override base class. void update(float dt); public: //implement interface for IGameLogic virtual void EnterGame() { //获取PlayScene的背景图片 const char* szPlaySceneBg = config::CGameConfig::getPlaySceneBg(); m_pGameScene->setBackground(szPlaySceneBg); //设置游戏状态为EGS_GAME_PLAY m_eGameState = EGS_GAME_PLAY; createGameObject("test_obj"); //emit signal... to notice that game is enter succesfully. Event_EnterGameSuccessed(); } virtual void ExitGame() { shutdown(); } public: bool init(CCScene* pScene); void shutdown() { //to do.. //exit. CCDirector::sharedDirector()->end(); } private: CGameObject* createGameObject(const char* szName); void destroyGameObject(CGameObject* pObj); }; } } #endif
本节源代码与工程文件:【资源引用页】
大家还可以尝试将UI Layout用XML实现。