Cocos2d-x CCNotifitionCenter类的使用介绍

发布时间:2020-09-29 09:46:10 作者:嗚拉呆
来源:网络 阅读:1003

       之前在学习cocos2d-x常用设计模式时,我们知道了一个相关的类CCNotifitionCenter,但当时那篇博文主要讲解观察者模式,对于CCNotifitionCenter类只是轻描淡写,之后我也在网上看了几篇关于此类的博文,但感觉都讲的不够全面,因此这里引用几篇相关博文,对他们做了一个总结,以得此文。


一.应用场合:

       游戏中经常遇到几个层之间互相通信的问题。比如,当前页是游戏页,游戏结束的时候,直接显示结算页,不进行转场了,结算页上又有按钮。这时如果要响应按钮点击事件处理一些数据之类的,需要包含别的头文件。这样不太好。像上边这种类似的问题,就可以使用cocos2d-x中提供的CCNotificationCenter这个类,用作通知中心。


二.使用介绍:

1. 在需要发送消息的地方(比如点击下一关按钮的回调函数),post消息即可,post有两种类型,带数据和不带数据:

void postNotification(const char *name);
void postNotification(const char *name, CCObject *object);

name:消息的名称,可以当成是消息的唯一标识,一般这样定义name:

constchar * constkXxxxName = "xxxx";

发送消息通知的例子如下:

extension::CCNotificationCenter::sharedNotificationCenter()->postNotification(kXxxxName);

注意:CCNotificationCenter是在extension命名空间中的,因此要记得使用命名空间extension,using namespace cocos2d::extension;


2. 在任何地方,只要你对某个消息感兴趣,你就可以在那里监听该消息。假如你对kXxxxName消息感兴趣,要做的:

首先,重写onEnter()(也可以是其他函数,只要保证在消息开始前被调用),在onEnter()里面监听消息,监听消息的原型如下:

void addObserver(CCObject *target, 
                     SEL_CallFuncO selector,
                     const char *name,
                     CCObject *obj);

具体实现代码如下:

void A::onEnter()

{    

   CCLayer::onEnter();    

   CCNotificationCenter::sharedNotificationCenter()->addObserver(          

           this,                                  

           callfuncO_selector(A::func),  // 处理的消息的回调函数

           kXxxxName,                    // 感兴趣的消息名称

           NULL);                        // 保存消息中传递的数据

}

这样的话如果有消息来得话,就会调用回调函数来处理了。

其次,也很重要,要重写onExit()方法,释放掉消息观察者,如果不释放,会发生内存泄露的,函数原型如下:

// name就是消息的名称,即要释放掉的那个消息
void removeObserver(CCObject *target,const char *name);
// 第二个方法:returns the number of observers removed
int removeAllObservers(CCObject *target);

具体实现代码如下:

void A::onExit()

{  

   CCNotificationCenter::sharedNotificationCenter()->removeObserver(      

           this,      

           kXxxxName);  // 消息名称

   CCLayer::onExit();

}


三.简单实例:

define CLICK_TEST_MSG "whatever_unique"   
                                                                                           
// 添加监听   
void GameMgr::addListener()
{  
    CCNotificationCenter* t_pNotiCenter = CCNotificationCenter::sharedNotificationCenter();  
    SEL_CallFuncO t_oCallFuncO = callfuncO_selector(GameMgr::onClickTest);  
    t_pNotiCenter->addObserver(this, t_oCallFuncO, CLICK_TEST_MSG, NULL);  
}  
                                                                                           
// 派发事件   
void GameMgr::dispatchEvent()
{  
    CCNode* t_pNode = new CCNode();  
    CCString* t_pCcStrMsg = new CCString("i love u!");  
    t_pNode->setUserData(t_pCcStrMsg);  
                                                                                               
    CCNotificationCenter* t_pNotiCenter = CCNotificationCenter::sharedNotificationCenter();  
    t_pNotiCenter->postNotification(CLICK_TEST_MSG, (CCObject*)t_pNode);  
}  
                                                                                           
// 事件响应   
void GameMgr::onClickTest(CCObject* in_pCcObjData)
{  
    CCNode* t_pNode = (CCNode*)in_pCcObjData;  
    CCString* t_pCcStrMsg = (CCString*)t_pNode->getUserData();  
    CCMessageBox(t_pCcStrMsg->getCString(), "Message");  
                                                                                               
    // 传递完毕不要忘记释放内存   
    t_pCcStrMsg->release();  
    t_pNode->release();  
}  
                                                                                           
// 移除监听   
void GameMgr::removeListener()
{  
    CCNotificationCenter* t_pNotiCenter =     CCNotificationCenter::sharedNotificationCenter();  
    t_pNotiCenter->removeObserver(this, CLICK_TEST_MSG);  
}

define CLICK_TEST_MSG "whatever_unique"   
                                                                                            
// 添加监听(可以选择在OnEnter内调用它)
void GameMgr::addListener()
{  
    CCNotificationCenter* t_pNotiCenter = CCNotificationCenter::sharedNotificationCenter();  
    SEL_CallFuncO t_oCallFuncO = callfuncO_selector(GameMgr::onClickTest);  
    t_pNotiCenter->addObserver(this, t_oCallFuncO, CLICK_TEST_MSG, NULL);  
}  
                                                                                            
// 派发事件   
void GameMgr::dispatchEvent()
{  
    CCNode* t_pNode = new CCNode();  
    CCString* t_pCcStrMsg = new CCString("i love u!");  
    t_pNode->setUserData(t_pCcStrMsg);  
                                                                                                
    CCNotificationCenter* t_pNotiCenter = CCNotificationCenter::sharedNotificationCenter();  
    t_pNotiCenter->postNotification(CLICK_TEST_MSG, (CCObject*)t_pNode);  
}  
                                                                                            
// 事件响应   
void GameMgr::onClickTest(CCObject* in_pCcObjData)
{  
    CCNode* t_pNode = (CCNode*)in_pCcObjData;  
    CCString* t_pCcStrMsg = (CCString*)t_pNode->getUserData();  
    CCMessageBox(t_pCcStrMsg->getCString(), "Message");  
                                                                                                
    // 传递完毕不要忘记释放内存   
    t_pCcStrMsg->release();  
    t_pNode->release();  
}  
                                                                                            
// 移除监听(可以选择在OnExit内调用它)
void GameMgr::removeListener()
{  
    CCNotificationCenter* t_pNotiCenter =     CCNotificationCenter::sharedNotificationCenter();  
    t_pNotiCenter->removeObserver(this, CLICK_TEST_MSG);  
}


四.实践补充:

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

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

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

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


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

void HelloWorld::displayCallBack(cocos2d::CCNode *pNode)
{
    //发送消息
    CCNotificationCenter::sharedNotificationCenter()->postNotification(MNOTIFICATION, (CCObject*)1);
    CCScene* scene = TargetRunScene::scene();
    CCDirector::sharedDirector()->pushScene(CCTransitionSlideInL::create(0.8f, scene));
}


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

那么,如何解决呢?

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

void HelloWorld::displayCallBack(cocos2d::CCNode *pNode)
{
    CCScene* scene = TargetRunScene::scene();
    CCDirector::sharedDirector()->pushScene(CCTransitionSlideInL::create(0.8f, scene));
    //发送消息
    CCNotificationCenter::sharedNotificationCenter()->postNotification(MNOTIFICATION, (CCObject*)1);
}


推荐阅读:
  1. Cocos2d-x 3.0标签类Label
  2. Cocos2d-x中的字符串

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cocos2d-x ccnotifitioncenter

上一篇:Python爬虫库BeautifulSoup获取对象名,属性,内容,注释

下一篇:python学生管理系统

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》