2013-12-12 156 views
0

我是Cocos2d-X的新手。通過CCNotficationCenter傳遞數據

CCNotificationCenter::sharedNotificationCenter()->addObserver(
      this, 
      callfuncO_selector(test::printSomething), 
      "hello", 
      NULL); 

和回調函數是

void PingoScreen::printSomething(CCObject *pObject) { 
    CCString * myData = (CCString*)pObject; 
    CCLog("The data posted is %s",myData); 
} 

現在我想通過通知發送CCString參數,以便

CCNotificationCenter::sharedNotificationCenter()->postNotification("hello", 
       ccs(notificationData)); 

我怎樣才能做到這一點?通知定義中需要更改什麼?

回答

1

註冊通知

CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(GameScene::doSomething), "eventNotification", NULL); 

移除通知

CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, "eventNotification"); 

郵政通知

CCNotificationCenter::sharedNotificationCenter()->postNotification("eventNotification", myString); 

回調方法

void GameScene::doSomething(CCObject *pObject) { 
    CCString *myString = (CCString*)pObject; 

    // XXX: Do something 
} 
+0

是什麼最後的NULL意味着在註冊通知碼? – AndroidDev

+1

addObserver中的額外對象參數用作過濾器。如果設置爲對象,則只有由此發件人發送的通知纔會傳送給觀察者。如果設置爲NULL,則此類型的所有通知將傳遞給觀察者。 – nomann