美文网首页
cocos --notification center

cocos --notification center

作者: godnotking | 来源:发表于2016-10-11 00:13 被阅读0次

本文参考Cocos2d-x CCNotificationCenter 通知中心


  • 使用意义

在不同的类之间传递参数,比如两个不同的layer之间参数的传递,一个layer调用另一个layer上的函数。


  • 使用方法

这个类位于cocos2dx/support
这个是一个单例类
单例对象的获取:

/** Gets the single instance of CCNotificationCenter. */ static CCNotificationCenter *sharedNotificationCenter(void);

添加监听:

void addObserver (CCObject *target, SEL_CallFuncO selector, const char* name, CCObject *obj);

例如:

CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::myNotification), "customString", NULL); //handle notification void HelloWorld::myNotification(CCObject *obj) { CCLog("Get Notificaiton ID:%d", (int)obj); }

发送通知:

void postNotification(const char *name); void postNotification(const char *name, CCObject *object);

例如:

CCNotificationCenter::sharedNotificationCenter()->postNotification("customString", (CCObject*)1);


注意:一般的在接受通知的一方在接受完通知后需要remove监听
一定要先注册监听,然后发送消息,这样才可以实现数据的传递。

相关文章

网友评论

      本文标题:cocos --notification center

      本文链接:https://www.haomeiwen.com/subject/njbvyttx.html