首页 > 代码库 > cocos2d-x 观察者设计模式
cocos2d-x 观察者设计模式
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
- {
- //////////////////////////////////////////////////////////////////////////
- // super init first
- //////////////////////////////////////////////////////////////////////////
- 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 观察者设计模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。