cocos2dx3.3开发FlappyBird总结十一:控制层功能设计

cocos2dx3.3开发FlappyBird总结十一:控制层功能设计,第1张

概述控制层的任务就是监听触摸事件,然后回调代理方法。控制层并不具体处理任务事情,只是抛给代理处理,因此需要先设计一个代理。 代理只是一个方法,那就是触摸: /** * The delegate between option layer and game layer */class OptionDelegate {public: /** * When touch the option layer

控制层的任务就是监听触摸事件,然后回调代理方法。控制层并不具体处理任务事情,只是抛给代理处理,因此需要先设计一个代理。

代理只是一个方法,那就是触摸:

/** * 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);这行了吗,这是设置代理,如果阅读不懂什么是代理,那先去找相关文章阅读。如果读者做过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总结十一:控制层功能设计所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1050086.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存