2013-12-12 214 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