通过CCNotificationCenter传递数据

Passing data with CCNotficationCenter

本文关键字:数据 CCNotificationCenter 通过      更新时间:2023-10-16

我是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));

我该怎么做?我需要更改通知定义中的哪些内容?

注册通知

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
}