Passing data with CCNotficationCenter

…衆ロ難τιáo~ 提交于 2019-12-25 02:39:21

问题


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 ?


回答1:


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
}


来源:https://stackoverflow.com/questions/20537329/passing-data-with-ccnotficationcenter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!