Cocos2dx(1) 设计模式-观察者模式 CCNotificationCenter
设计模式-观察者模式 CCNotificationCenter
一般用作通知中心
实现步骤:
1、添加观察者:
在任何地方,只要你你对某个消息感兴趣(和pureMVC中的listNotification一样),你就可以在那里监听该消息。
void addObserver(CCObject* target,SEL_CallFuncO callBack,const char* name, CCObject* data);
参数1为事件监听的目标,参数2为回调函数(即接收到消息后执行的函数),参数3为消息名,参数4为消息体。
具体实现如下:
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfunco_selector(GameLayer::callBack), name, NULL
2、发送消息:
在需要发送消息的地方,调用postNotification方法即可,postNotification有两种,可以不带数据和带数据。
void postNotification(const char* name); void postNotification(const char* name,CCObject* data);
name是消息名,是消息唯一标识,在整个游戏过程中是唯一的,因此,我们一般把所有的消息名放在一个头文件中,纺织消息名重复,data是消息体,即发送的数据。
发送通知如下:
CCNotificationCenter::sharedNotificationCenter()->postNotification(name);
3、释放消息观察者:
释放消息观察者是很重要的,不释放的话,会产生内存泄露。我们需要在onExit()方法里面,释放消息观察者。
void GameLayer::onExit(){ CCLayer::onExit(); CCNotificationCenter::sharedNotificationCenter()->removeObserver( this, name //消息名 ); }
转自:http://wuxinwei1023.blog.163.com/blog/static/267998352013119102912716/
二、之后的事
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);
//CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this); }
然后写个小程序来实践下这个。
改用系统为我们创建的Helloworld,首先将系统中建立退出按钮的代码注释掉。
然后Helloworld::init中末尾这样写
CCSprite *button = CCSprite::create("CloseNormal.png"); button->setPosition(ccp(200,300)); CCNode* t_pNode = new CCNode(); t_pNode->setUserData(button); CCNotificationCenter* t_pNotiCenter = CCNotificationCenter::sharedNotificationCenter(); t_pNotiCenter->addObserver(this, callfuncO_selector(HelloWorld::onClickTest), CLICK_TEST_MSG, NULL); CCNotificationCenter::sharedNotificationCenter()->postNotification(CLICK_TEST_MSG, (CCObject*)t_pNode); this->addChild(button);
最后添加响应函数
void HelloWorld::onClickTest(CCObject* obj) { CCNode *temp =(CCNode*)obj; CCSprite *button =(CCSprite*)temp->getUserData(); button->setPosition(ccp(100,100)); }
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号