使用CCNotficationCenter传递数据

时间:2013-12-12 07:20:58

标签: c++ cocos2d-x

我是Cocos2d-X的新手。

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 个答案:

答案 0 :(得分: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
}