加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2dx 学习笔记26 CCNotificationCenter的使用

发布时间:2020-12-14 19:22:22 所属栏目:百科 来源:网络整理
导读:在ios开发中,经常会使用到通知这种模式,在coscos2dx中也移植了这种模式,CCNotificationCenter CCNotificationCenter被设计成了一个单例模式。其使用方法和OC中差不多,在ios开发中经常是再一个界面出现或初始化的时候,注册消息通知,在界面消失时取消通

在ios开发中,经常会使用到通知这种模式,在coscos2dx中也移植了这种模式,CCNotificationCenter

CCNotificationCenter被设计成了一个单例模式。其使用方法和OC中差不多,在ios开发中经常是再一个界面出现或初始化的时候,注册消息通知,在界面消失时取消通知。CCNotificationCenter的用法如下:

先来看下cocos2dx中注册通知的函数:

/** @brief Adds an observer for the specified target.
* @param target The target which wants to observe notification events.
* @param selector The callback function which will be invoked when the specified notification event was posted.
* @param name The name of this notification.
* @param obj The extra parameter which will be passed to the callback function.
*/
void addObserver(CCObject *target,
SEL_CallFuncO selector,
const char *name,
CCObject *obj);


由此可见我们该这样来添加一个观察者:

CCNotificationCenter::sharedNotifCenter()->addObserver(this,callfuncO_selector(HelloWorld::menuCloseCallback),TONGZHI,NULL); //注册消息


void HelloWorld::menuCloseCallback(CCObject* pSender) { //移除监听事件 CCLog("收到监听 并且移除!"); CCNotificationCenter::sharedNotifCenter()->removeObserver(this,TONGZHI); } 写好以后 只要在需要的地方抛出通知 即可 CCNotificationCenter::sharedNotifCenter()->postNotification(TONGZHI,NULL);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读