标签:
1、参考文章 http://blog.csdn.net/vanquishedzxl/article/details/23616535
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- void sengMsg(CCObject *pSender);
- void testMSG(CCObject *pSender);
- CREATE_FUNC(HelloWorld);
- };
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
-
-
-
-
- CC_BREAK_IF(! CCLayer::init());
- CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(HelloWorld::testMSG),"test",NULL);
- CCMenuItemLabel *labelItem = CCMenuItemLabel::create(CCLabelTTF::create("Send MSG","Arial",26),this,menu_selector(HelloWorld::sengMsg));
- CCMenu *menu = CCMenu::create(labelItem,NULL);
- this->addChild(menu);
- bRet = true;
- } while (0);
-
- return bRet;
- }
- void HelloWorld::sengMsg(CCObject *pSender)
- {
- CCLOG("sendMSG");
- CCNotificationCenter::sharedNotificationCenter()->postNotification("test",NULL);
- }
- void HelloWorld::testMSG(CCObject *pSender)
- {
- CCLOG("testMSG");
- }
--实际用法demo
#define EventManager NotificationCenter::getInstance()
void PlayScene::init_listen(){
EventManager->addObserver(this, CC_CALLFUNCO_SELECTOR(PlayScene::handler_refresh_hand_card), GameEvent::handler_refresh_hand_card, NULL);
EventManager->addObserver(this, CC_CALLFUNCO_SELECTOR(PlayScene::handler_refresh_all_card), GameEvent::handler_refresh_all_card, NULL);
}
EventManager->postNotification(GameEvent::on_msg, sm);
EventManager->removeAllObservers(this);
cocos2d-x 观察者设计模式
标签:
原文地址:http://www.cnblogs.com/puyahuazhijia/p/5950448.html