Cocos2d-x CCNotificationCenter 通知中心 自定义消息事件

本文转自小邓笔记,原文链接:http://blog.csdn.net/crayondeng/article/details/11677457


感谢原作者的精彩分析。


Cocos2d-x 的世界里面封装的和C++的消息队列一样的事件处理中心。在编写cocos2d游戏的时候在合适的地方发送自定义消息是非常需要的功能,当然在cocos里面也十分简单好用。 

相信接触过ios开发的人来说对NSNotificationCenter都不陌生。而在cocos2d-x中也参照这个类,提供了CCNotificationCenter这个类,用作通知中心。

那么NotificationCenter的介绍请看这篇文章:点击打开链接

而我主要是使用NotificationCenter 进行不同类之间的参数传递。(譬如说在两个layer之间进行参数的传递)

下面对这个CCNotificationCenter类如何使用进行简单的介绍。

1、首先这个类的位置:cocos2dx/support

2、

注意这是一个单例类

使用时要获取到单例对象:

[cpp]  view plain copy
  1. /** Gets the single instance of CCNotificationCenter. */  
  2.     static CCNotificationCenter *sharedNotificationCenter(void);  

发送通知:

主要用到的两个方法:

[cpp]  view plain copy
  1. void postNotification(const char *name);  
  2.   
  3. void postNotification(const char *name, CCObject *object);  

例子:

[cpp]  view plain copy
  1. // Define this at the header  
  2. #define MY_NOTIFICATION "MY_NOTIFICATION"  

[cpp]  view plain copy
  1. CCNotificationCenter::sharedNotificationCenter()->postNotification(MY_NOTIFICATION, (CCObject*)1);  

接收通知(添加监听):

方法:

[cpp]  view plain copy
  1. void addObserver(CCObject *target,   
  2.                      SEL_CallFuncO selector,  
  3.                      const char *name,  
  4.                      CCObject *obj);  

例子:

[cpp]  view plain copy
  1. CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::myNotification), MY_NOTIFICATION, NULL);  

[cpp]  view plain copy
  1. // Handle the notification  
  2. void HelloWorld::myNotification(CCObject* obj)  
  3. {  
  4.     CCLOG("Notification achieved. ID: %i", (int)obj);  
  5. }  

注意:一般的在接受通知的一方在接受完通知后需要remove监听。

方法:

[cpp]  view plain copy
  1. void removeObserver(CCObject *target,const char *name);  
  2.   
  3. int removeAllObservers(CCObject *target);  

(注意第二个方法: returns the number of observers removed)

例子:

[cpp]  view plain copy
  1. HelloWorld::~HelloWorld()  
  2. {  
  3.     CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MY_NOTIFICATION);  
  4.       
  5. //    CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this);  
  6. }  

很简单吧!

参考文章: http://www.plungeinteractive.com/blog/2012/09/20/notification-center-extension-for-cocos2d-x/


补充 :

最近在使用CCNotificationCenter 在两个 scene 之间传递参数的过程中遇到一个很容易出错的细节,下面简单记录一下。

我们知道,使用 CCNotificationCenter 在两个 scene 之间传递参数,接受方scene 要添加监听,也就是 addObserver ;而发送方scene是要发送消息,也就是 postNotification。

那么二者有先后的顺序吗?

注意:一定要先注册监听,然后发送消息,这样才可以实现数据的传递。--- 当然,这个也是很容易理解的吧。


而我就恰恰没有注意到这个问题,所以导致无法传递数据。

[cpp]  view plain copy
  1. <pre name="code" class="cpp">void HelloWorld::displayCallBack(cocos2d::CCNode *pNode)  
  2. {  
  3.     //发送消息  
  4.     CCNotificationCenter::sharedNotificationCenter()->postNotification(MNOTIFICATION, (CCObject*)1);  
  5.       
  6.     CCScene* scene = TargetRunScene::scene();  
  7.     CCDirector::sharedDirector()->pushScene(CCTransitionSlideInL::create(0.8f, scene));  
  8. }</pre>  


注意到:消息的接受方是 TargetRunScene ,那么我现实发送了消息,然后才初始化接受方的 scene,那么显然接收方的添加监听是在发送了消息后的,所以这样的话,是无法传递参数的。

那么,如何解决呢?

简单啦,就是讲发送消息的放在TargetRunScene 初始化之后就可以了。

[cpp]  view plain copy
  1. void HelloWorld::displayCallBack(cocos2d::CCNode *pNode)  
  2. {  
  3.     CCScene* scene = TargetRunScene::scene();  
  4.     CCDirector::sharedDirector()->pushScene(CCTransitionSlideInL::create(0.8f, scene));  
  5.       
  6.     //发送消息  
  7.     CCNotificationCenter::sharedNotificationCenter()->postNotification(MNOTIFICATION, (CCObject*)1);  
  8. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值