cocos2dx3.3开发FlappyBird总结十一:控制层功能设计
2015-03-20 11:44
423 查看
控制层的任务就是监听触摸事件,然后回调代理方法。控制层并不具体处理任务事情,只是抛给代理处理,因此需要先设计一个代理。
代理只是一个方法,那就是触摸:
代理类被声明为纯虚函数,这样自己是不能实现的,且遵守此代理的类,必须要实现此代理中的方法。
下面说说控制层:
看到CC_SYNTHESIZE(OptionDelegate*, _optionDelegate, OptionDelegate);这行了吗,这是设置代理,如果阅读不懂什么是代理,那先去找相关文章阅读。如果读者做过IOS开发,那么一定很熟悉,因为那是最常用的通信机制。
简单来说,这里控制层监听触摸事件,然后控制层就告诉代理,我监听到触摸事件了,那么代理实现对应的方法,就可以了。
在初始化时,让控制层添加监听事件:
好了,下一步该说说状态层了
代理只是一个方法,那就是触摸:
/** * The delegate between option layer and game layer */ class OptionDelegate { public: /** * When touch the option layer, it will be called */ virtual void onTouch() = 0; };
代理类被声明为纯虚函数,这样自己是不能实现的,且遵守此代理的类,必须要实现此代理中的方法。
下面说说控制层:
class OptionDelegate; /** * The game background,showing the background information * in the game. */ class OptionLayer : public cocos2d::Layer { public: /** * The default constructor */ OptionLayer(); /** * The default destructor */ ~OptionLayer(); /** * The init method, will init the super init method first * * @return true if succeeded, otherwise false */ virtual bool init(); CREATE_FUNC(OptionLayer); /** * Override the multitouch method */ virtual void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event); /** * The delegate */ CC_SYNTHESIZE(OptionDelegate*, _optionDelegate, OptionDelegate); };
看到CC_SYNTHESIZE(OptionDelegate*, _optionDelegate, OptionDelegate);这行了吗,这是设置代理,如果阅读不懂什么是代理,那先去找相关文章阅读。如果读者做过IOS开发,那么一定很熟悉,因为那是最常用的通信机制。
简单来说,这里控制层监听触摸事件,然后控制层就告诉代理,我监听到触摸事件了,那么代理实现对应的方法,就可以了。
在初始化时,让控制层添加监听事件:
bool OptionLayer::init() { if (!Layer::init()) { return false; } // Register touches began event auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(OptionLayer::onTouchesBegan, this); // // 这个是3.0后的新特性,这里就没有使用,主要还是想先了解旧方式,但新的方式也是需要知道的 // listener->onTouchesBegan = [&](const std::vector<Touch *> &touches, Event *event) { // CCLOG("touches begin"); // if (this->getOptionDelegate()) { // this->getOptionDelegate()->onTouch(); // } // }; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; }
好了,下一步该说说状态层了
相关文章推荐
- cocos2dx3.3开发FlappyBird总结十:背景层设计
- cocos2dx3.3开发FlappyBird总结十二:状态层设计
- cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角)
- cocos2dx3.3开发FlappyBird总结十六:游戏层实现
- cocos2dx3.3开发FlappyBird总结五:说说屏幕适配
- cocos2dx3.3开发FlappyBird总结九:欢迎场景(WelcomeScene)
- cocos2dx3.3开发FlappyBird总结十七:结束语
- cocos2dx3.3开发FlappyBird总结一:背景介绍
- cocos2dx3.3开发FlappyBird总结十四:常量定义
- cocos2dx3.3开发FlappyBird总结八:载入场景LoadingScene
- cocos2dx3.3开发FlappyBird总结四:资源管理类
- cocos2dx3.3开发FlappyBird总结十五:记录玩家得分
- cocos2dx3.3开发FlappyBird总结三:项目剖析
- cocos2dx3.3开发FlappyBird总结七:说说游戏流程
- cocos2dx3.3开发FlappyBird总结十三:数字特效类
- App后台开发运维和架构实践学习总结(4)——APP的注册和登录功能设计
- App后台开发运维和架构实践学习总结(4)——APP的注册和登录功能设计
- WinForm控件开发总结(十一)-----调试控件的设计时行为
- cocos2dxFlappyBird开发总结二:开发环境介绍
- 《游戏脚本的设计与开发》-第一章总结 文字脚本的功能扩展和一个游戏测试