cocos2d-x 观察者设计模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 观察者设计模式的主要内容,如果未能解决你的问题,请参考以下文章