尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17150705
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人13】完善鱼群效果
1、大炮功能分析
基本数据
等级
外观(外观由等级决定)
基本操作
等级改变
炮弹发射
2、大炮布局与事件
额外工作
主界面布局
大炮相关
降级按钮
升级按钮
大炮炮管
具体的实现如下:
初始化player界面层
#ifndef __GAME_UI_MGR_H__ #define __GAME_UI_MGR_H__ #include "../ui/UIBeginLogic.h" #include "../ui/UIPlayLogic.h" #include "../include/IGameLogic.h" namespace fishingjoy { namespace logic { class CGameUIMgr { ui::CBeginUILogic* m_pBeginUI; ui::CPlayUILogic* m_pPlayUI; public: CGameUIMgr() { } void init(IGameLogic* pLogic,CCLayer* pUIRoot) { m_pBeginUI = new ui::CBeginUILogic(pLogic,pUIRoot); //初始化player界面 m_pPlayUI = new ui::CPlayUILogic(pLogic,pUIRoot); } }; } } #endif大炮界面层实现:
#ifndef __UI_CANNON_LAYOUT_H__ #define __UI_CANNON_LAYOUT_H__ #include "cocos2d.h" #include "../include/SignalSlot.h" USING_NS_CC; namespace fishingjoy { namespace ui { class UI_CannonLayout:public CCLayer { public: base::Signal<void(void)> Event_CannonAdd; base::Signal<void(void)> Event_CannonReduce; CCSprite* m_pCannon; public: UI_CannonLayout() { m_pCannon = CCSprite::create("ui/pao5.png",CCRectMake(0,0,64,88)); //bg->setAnchorPoint(CCPointZero); addChild(m_pCannon); //add button. CCMenuItemSprite* pAdd = CCMenuItemSprite::create(CCSprite::create("ui/button_add.png",CCRectMake(0,0,34.5,42)) ,CCSprite::create("ui/button_add.png",CCRectMake(34.5,0,34.5,42)),this,menu_selector(UI_CannonLayout::onMenu_Add)); pAdd->setPositionX(50); //reduce button. CCMenuItemSprite* pReduce = CCMenuItemSprite::create(CCSprite::create("ui/button_reduce.png",CCRectMake(0,0,34.5,42)) ,CCSprite::create("ui/button_reduce.png",CCRectMake(34.5,0,34.5,42)),this,menu_selector(UI_CannonLayout::onMenu_Reduce)); pReduce->setPositionX(-50); //add all of buttons into menu. CCMenu* pMenu = CCMenu::create(pAdd,pReduce,NULL); pMenu->setPositionX(0); pMenu->setPositionY(0); //attach to ui node. addChild(pMenu); } void setCannonLevel(int lv) { m_pCannon->setTextureRect(CCRectMake((lv-1)*64,0,64,88)); } private: void onMenu_Add(CCObject* pSender) { Event_CannonAdd(); } void onMenu_Reduce(CCObject* pSender) { Event_CannonReduce(); } }; } } #endif
#ifndef __PLAY_UI_LOGIC_H__ #define __PLAY_UI_LOGIC_H__ #include "UIPlayLayout.h" #include "UICannonLayout.h" #include "../include/IGameLogic.h" namespace fishingjoy { namespace ui { class CPlayUILogic { UI_PlayLayout m_PlayLayout; UI_CannonLayout m_CannonLayout; logic::IGameLogic* m_pGameLogic; int m_iCannonLevel; public: CPlayUILogic(logic::IGameLogic* p,cocos2d::CCLayer* pParent) { m_pGameLogic = p; pParent->addChild(&m_PlayLayout); m_PlayLayout.setVisible(false); base::Delegate<void(void)> egs(this,&CPlayUILogic::slot_EnterGame);; m_pGameLogic->Event_EnterGameSuccessed+=egs; base::Delegate<void(void)> gps(this,&CPlayUILogic::slot_CannonAdd);; m_CannonLayout.Event_CannonAdd += gps; base::Delegate<void(void)> ges(this,&CPlayUILogic::slot_CannonReduce); m_CannonLayout.Event_CannonReduce += ges; m_PlayLayout.addChild(&m_CannonLayout); m_CannonLayout.setPositionX(258); m_CannonLayout.setPositionY(25); m_iCannonLevel = 1; } private: void slot_EnterGame() { m_PlayLayout.setVisible(true); } void slot_CannonAdd() { m_iCannonLevel++; if(m_iCannonLevel > 5) m_iCannonLevel = 1; m_CannonLayout.setCannonLevel(m_iCannonLevel); } void slot_CannonReduce() { m_iCannonLevel--; if(m_iCannonLevel < 1) m_iCannonLevel = 5; m_CannonLayout.setCannonLevel(m_iCannonLevel); } }; } } #endif
现在可以看到大炮界面了:
本节源代码与工程文件:【资源引用页】