您的位置:首页 > 移动开发 > Cocos引擎

【cocos2d-x IOS游戏开发-捕鱼达人4】基本游戏框架

2013-12-05 14:39 537 查看
尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17140521

上节回顾

【cocos2d-x IOS游戏开发-捕鱼达人3】交叉编译环境搭建




1、游戏组成要素

常规分类

       主程序

              由C/C++等语言编译生成的可执行文件。 如WINDOWS下的EXE

       资源
              脚本:如 Python,Lua,JS等。
              模型:图片、声音:提供游戏的表现资源
              配置:灵活地制定游戏内容。

主程序特点

游戏是一个死循环,仅当玩家退出程序时才终止

游戏是由一个又一个状态机组成的。 如场景切换,角色控制等。

每当有修改时,需要重新编译并发布。

配置文件

配置文件能够在不重新编译游戏程序的前提下,提供灵活的游戏扩展性

配置文件可以是文本格式,XML格式,也可以是自定义的二进制格式。

脚本

脚本能够在提供配置能力的同时,更高层次地修改游戏内容。而不用重新编译游戏程序

2、基本游戏框架的实现

Scene

负责处理场景相关的显示

UI

UI显示相关的代码

Config

负责资源和配置信息的加载

Logic

游戏状态管理、流程控制等

我们来看具体的代码实现:

bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
//     pDirector->enableRetinaDisplay(true);

// turn on display FPS
pDirector->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);

static CCScene* pScene = CCScene::create();

//初始化游戏逻辑控制
if(GGameLogic.init(pScene)==false)
return false;

//The 'update' selector will be called every frame.
//The lower the priority, the earlier it is called.
pDirector->getScheduler()->scheduleUpdateForTarget(&GGameLogic,0,false);

// run
pDirector->runWithScene(pScene);

return true;
}


再看具体的游戏逻辑实现:

#ifndef _GAME_LOGIC_H_
#define _GAME_LOGIC_H_
#include "cocos2d.h"
USING_NS_CC;

#include "GameScene.h"
class CGameLogic:public cocos2d::CCObject
{
CGameScene* m_pGameScene;
public:
//override base class.
void update(float dt)
{
if(m_pGameScene)
m_pGameScene->update(dt);
}

public:
bool init(CCScene* pScene)
{
if(pScene==NULL)
return false;
//初始化m_pGameScene
m_pGameScene = CGameScene::create();
if(m_pGameScene==NULL)
return false;
//将m_pGameScene加入导演控制的主界面pScene
pScene->addChild(m_pGameScene);

return true;
}
void shutdown()
{
//to do..

//exit.
CCDirector::sharedDirector()->end();
}
};
#endif


【思考】为什么场景,UI,配置,逻辑要分开处理,而不是写在一起。 这样有什么额外的负担,又会带来什么好处?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息