Cocos2dx 学习笔记26 CCNotificationCenter的使用
在ios开发中,经常会使用到通知这种模式,在coscos2dx中也移植了这种模式,CCNotificationCenter CCNotificationCenter被设计成了一个单例模式。其使用方法和OC中差不多,在ios开发中经常是再一个界面出现或初始化的时候,注册消息通知,在界面消失时取消通知。CCNotificationCenter的用法如下: 先来看下cocos2dx中注册通知的函数: /** @brief Adds an observer for the specified target.
由此可见我们该这样来添加一个观察者: 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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |