zoukankan      html  css  js  c++  java
  • cocos2d-x 观察者设计模式

    1、参考文章  http://blog.csdn.net/vanquishedzxl/article/details/23616535

    1. class HelloWorld : public cocos2d::CCLayer  
    2. {  
    3. public:  
    4.     virtual bool init();    
    5.     static cocos2d::CCScene* scene();  
    6.     void  sengMsg(CCObject *pSender);  
    7.     void testMSG(CCObject *pSender);  
    8.     CREATE_FUNC(HelloWorld);  
    9. }; 
    1. bool HelloWorld::init()  
    2. {  
    3.     bool bRet = false;  
    4.     do   
    5.     {  
    6.         //////////////////////////////////////////////////////////////////////////  
    7.         // super init first  
    8.         //////////////////////////////////////////////////////////////////////////  
    9.   
    10.         CC_BREAK_IF(! CCLayer::init());  
    11.         CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(HelloWorld::testMSG),"test",NULL);  
    12.         CCMenuItemLabel *labelItem = CCMenuItemLabel::create(CCLabelTTF::create("Send MSG","Arial",26),this,menu_selector(HelloWorld::sengMsg));  
    13.         CCMenu *menu = CCMenu::create(labelItem,NULL);  
    14.         this->addChild(menu);  
    15.         bRet = true;  
    16.     } while (0);  
    17.   
    18.     return bRet;  
    19. }  
    20. void HelloWorld::sengMsg(CCObject *pSender)  
    21. {  
    22.     CCLOG("sendMSG");  
    23.     CCNotificationCenter::sharedNotificationCenter()->postNotification("test",NULL);  
    24. }  
    25. void HelloWorld::testMSG(CCObject *pSender)  
    26. {  
    27.     CCLOG("testMSG");  
    28. }  

    --实际用法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);

  • 相关阅读:
    C++---拷贝构造函数和赋值构造函数
    C++---类成员变量定义为引用
    从文件处理到文件的高级应用
    Jupyter的使用复习
    字符编码到python编辑器流程
    周四的小结
    中秋前的题目
    三段代码块带走今天的脚本
    今日份的随笔
    明天才能学的运算符号表格
  • 原文地址:https://www.cnblogs.com/puyahuazhijia/p/5950448.html
Copyright © 2011-2022 走看看