Cocos2dx 学习笔记26 CCNotificationCenter的使用

在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);

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...