Cocos2d-x CCNotificationCenter 通知中心
2013-09-15 20:41
330 查看
相信接触过ios开发的人来说对NSNotificationCenter都不陌生。而在cocos2d-x中也参照这个类,提供了CCNotificationCenter这个类,用作通知中心。
那么NotificationCenter的介绍请看这篇文章:点击打开链接
而我主要是使用NotificationCenter 进行不同类之间的参数传递。(譬如说在两个layer之间进行参数的传递)
下面对这个CCNotificationCenter类如何使用进行简单的介绍。
1、首先这个类的位置:cocos2dx/support
2、
注意这是一个单例类
使用时要获取到单例对象:
发送通知:
主要用到的两个方法:
例子:
接收通知(添加监听):
方法:
例子:
注意:一般的在接受通知的一方在接受完通知后需要remove监听。
方法:
(注意第二个方法: returns the number of observers removed)
例子:
很简单吧!
参考文章: http://www.plungeinteractive.com/blog/2012/09/20/notification-center-extension-for-cocos2d-x/
那么NotificationCenter的介绍请看这篇文章:点击打开链接
而我主要是使用NotificationCenter 进行不同类之间的参数传递。(譬如说在两个layer之间进行参数的传递)
下面对这个CCNotificationCenter类如何使用进行简单的介绍。
1、首先这个类的位置:cocos2dx/support
2、
注意这是一个单例类
使用时要获取到单例对象:
/** Gets the single instance of CCNotificationCenter. */ static CCNotificationCenter *sharedNotificationCenter(void);
发送通知:
主要用到的两个方法:
void postNotification(const char *name); void postNotification(const char *name, CCObject *object);
例子:
// Define this at the header #define MY_NOTIFICATION "MY_NOTIFICATION"
CCNotificationCenter::sharedNotificationCenter()->postNotification(MY_NOTIFICATION, (CCObject*)1);
接收通知(添加监听):
方法:
void addObserver(CCObject *target, SEL_CallFuncO selector, const char *name, CCObject *obj);
例子:
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::myNotification), MY_NOTIFICATION, NULL);
// Handle the notification void HelloWorld::myNotification(CCObject* obj) { CCLOG("Notification achieved. ID: %i", (int)obj); }
注意:一般的在接受通知的一方在接受完通知后需要remove监听。
方法:
void removeObserver(CCObject *target,const char *name); int removeAllObservers(CCObject *target);
(注意第二个方法: returns the number of observers removed)
例子:
HelloWorld::~HelloWorld() { CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MY_NOTIFICATION); // CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this); }
很简单吧!
参考文章: http://www.plungeinteractive.com/blog/2012/09/20/notification-center-extension-for-cocos2d-x/
相关文章推荐
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心 自定义消息事件
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
- cocos2d CCNotificationCenter
- cocos2d-x节点(CCNotificationCenter.h)API
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- cocos2d-x 血泪史(-1) CCNotificationCenter 与LUA
- Cocos2D-x游戏开发之二十三:CCNotificationCenter观察者模式(2)-不同层之间事件的发送和接受
- Cocos2d-x 中 CCNotificationCenter 的使用
- COCOS2D-X之CCNotificationCenter的简单使用Demo
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
- cocos2d-x CCNotificationCenter一个优化想法
- CCnotificationCenter 通知
- Cocos2d-x 2.x编程之CCNotificationCenter
- Cocos2d-x v3.x 的事件派发机制(消息处理机制CCNotificationCenter)
- Cocos2d-x CCNotificationCenter