尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17142061
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人5】C++中函数代理与信号插槽机制
1、菜单UI与事件
Cocos2d-x中的菜单
CCMenu
CCMenuItem*
Image
Label
Sprite
xxx
Cocos2d-x中的菜单事件
menu_selector
void (CCObject::*menu_callback_fun)(CCObject*);
UI组成
UI布局(Layout)
UI逻辑(Logic)
2、使用信号插槽机制
与UI有关的游戏组成部分
UI布局:负责UI定位和显示(通常交给配置文件做)
UI逻辑:负责接收UI事件,和逻辑事件,修改UI显示效果和调用游戏逻辑接口进行相应修改
游戏逻辑:负责游戏数据持有和游戏规则的执行,并抛出事件让UI逻辑监听
需要用到信号插槽机制的地方
UI布局的事件抛出 如:按钮被点击
游戏逻辑抛出事件,让UI逻辑监听 如:捕到鱼,显示积分。
我们来一步步实现开始菜单的代码:
1、实现AppDelegate::applicationDidFinishLaunching
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(); //初始化游戏逻辑,传入pScene if(GGameLogic.init(pScene)==false) return false; pDirector->getScheduler()->scheduleUpdateForTarget(&GGameLogic,0,false); // run pDirector->runWithScene(pScene); return true; }
2、实现游戏逻辑的总控制
#ifndef _GAME_LOGIC_H_ #define _GAME_LOGIC_H_ #include "cocos2d.h" USING_NS_CC; #include "../include/IGameScene.h" #include "../include/SignalSlot.h" #include "GameUIMgr.h" namespace fishingjoy { namespace logic { class CGameLogic:public cocos2d::CCObject,public IGameLogic { scene::IGameScene* m_pGameScene; CGameUIMgr mUIMgr; public: //override base class. void update(float dt) { if(m_pGameScene) m_pGameScene->update(dt); } public: bool init(CCScene* pScene) { //base::Delegate<void(void)> testd(this,&CGameLogic::shutdown); //testd(); //base::Signal<void(void)> s; //s+=testd; //s(); if(pScene==NULL) return false; m_pGameScene = scene::createScene(); if(m_pGameScene==NULL) return false; //创建一个CCLayer:pSceneRoot cocos2d::CCLayer* pSceneRoot = cocos2d::CCLayer::create(); pScene->addChild(pSceneRoot); m_pGameScene->attachTo(pSceneRoot); //设置背景照片 m_pGameScene->setBackground("media/begin.png"); //创建UI层 cocos2d::CCLayer* pUIRoot = cocos2d::CCLayer::create(); //将UI层加入pScene pScene->addChild(pUIRoot); //初始化CGameUIMgr UI控制类 mUIMgr.init(this,pUIRoot); return true; } void shutdown() { //to do.. //exit. CCDirector::sharedDirector()->end(); } }; } } #endif3、初始化CGameUIMgr UI控制类
#ifndef __GAME_UI_MGR_H__ #define __GAME_UI_MGR_H__ #include "../ui/UIBeginLogic.h" #include "../include/IGameLogic.h" namespace fishingjoy { namespace logic { class CGameUIMgr { ui::CBeginUILogic* m_pBeginUI; public: CGameUIMgr() { } void init(IGameLogic* pLogic,CCLayer* pUIRoot) { //初始化CBeginUILogic m_pBeginUI = new ui::CBeginUILogic(pLogic,pUIRoot); } }; } } #endif4、初始化CBeginUILogic:实现UI层逻辑
#ifndef __BEGIN_UI_LOGIC_H__ #define __BEGIN_UI_LOGIC_H__ #include "UIBeginLayout.h" #include "../include/IGameLogic.h" namespace fishingjoy { namespace ui { class CBeginUILogic { UI_BeginLayout m_BeginLayout; logic::IGameLogic* m_pGameLogic; public: CBeginUILogic(logic::IGameLogic* p,cocos2d::CCLayer* pParent) { m_pGameLogic = p; //UI控制层加入UI的BeginLayout层 pParent->addChild(&m_BeginLayout); //创建开始游戏的代理:打印play game.. base::Delegate<void(void)> gps(this,&CBeginUILogic::slot_PlayGame); //将代理加入Event_BeginGame槽 m_BeginLayout.Event_BeginGame += gps; //创建结束游戏的代理:打印exit game.. base::Delegate<void(void)> ges(this,&CBeginUILogic::slot_ExitGame); m_BeginLayout.Event_ExitGame += ges; } private: void slot_PlayGame() { printf("play game..."); } void slot_ExitGame() { printf("exit game..."); } }; } } #endif5、实现UI层的布局
#ifndef __UI_BEGIN_LAYOUT_H__ #define __UI_BEGIN_LAYOUT_H__ #include "cocos2d.h" #include "../include/SignalSlot.h" USING_NS_CC; namespace fishingjoy { namespace ui { class UI_BeginLayout:public CCLayer { public: base::Signal<void(void)> Event_BeginGame; base::Signal<void(void)> Event_ExitGame; public: UI_BeginLayout() { //begin button. CCMenuItemImage* pBeginGame = CCMenuItemImage::create("ui/game_start.png","ui/game_start2.png",this,menu_selector(UI_BeginLayout::onMenu_Begin)); pBeginGame->setPositionY(-40); //exit button. CCMenuItemImage* pExitGame = CCMenuItemImage::create("ui/game_exit.png","ui/game_exit2.png",this,menu_selector(UI_BeginLayout::onMenu_Exit)); pExitGame->setPositionY(-80); //add all of buttons into menu. CCMenu* pMenu = CCMenu::create(pBeginGame,pExitGame,NULL); //attach to ui node. addChild(pMenu); } private: void onMenu_Begin(CCObject* pSender) { Event_BeginGame(); } void onMenu_Exit(CCObject* pSender) { Event_ExitGame(); } }; } } #endif
当点击开始游戏按钮会打印play game,点击退出游戏按钮会打印exit game
OpenGL version = 4.2.11566 Compatibility Profile Context Ready for GLSL Ready for OpenGL 2.0 <dict> cocos2d.x.version: 2.2.1 cocos2d.x.compiled_with_profiler: false cocos2d.x.compiled_with_gl_state_cache: true gl.vendor: ATI Technologies Inc. gl.renderer: AMD Radeon HD 6450 gl.version: 4.2.11566 Compatibility Profile Context gl.max_texture_size: 16384 gl.max_texture_units: 32 gl.supports_PVRTC: false gl.supports_NPOT: true gl.supports_BGRA8888: false gl.supports_discard_framebuffer: false gl.supports_vertex_array_object: true </dict> play game...play game...play game...exit game...exit game...exit game...exit gam e...play game...play game...exit game...play game...exit game...
PS:大家一定要理解将UI布局,UI逻辑,游戏逻辑分离的必要性;\(^o^)/~