Search code examples
c++cocos2d-x

Passing data with CCNotficationCenter


I am new to Cocos2d-X.

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

and the callback function is

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

Now i want to send a CCString parameter via notification so that

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

How can i do this ? What do i need to change in the notification definition ?


Solution

  • Register Notification

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

    Remove Notification

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

    Post Notification

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

    Callback method

    void GameScene::doSomething(CCObject *pObject) {
        CCString *myString = (CCString*)pObject;
    
        // XXX: Do something
    }