Cocos2d-x CCNotificationCenter
来源:互联网 发布:淘宝退换货规则 编辑:程序博客网 时间:2024/06/04 18:17
使用NotificationCenter 进行不同类之间的参数传递。(譬如说在两个layer之间进行参数的传递)
下面对这个CCNotificationCenter类如何使用进行简单的介绍。
1、首先这个类的位置:cocos2dx/support
2、
注意这是一个单例类
使用时要获取到单例对象:
- /** Gets the single instance of CCNotificationCenter. */
- static CCNotificationCenter *sharedNotificationCenter(void);
发送通知:
主要用到的两个方法:
- void postNotification(const char *name);
- void postNotification(const char *name, CCObject *object);
例子:
- // Define this at the header
- #define MY_NOTIFICATION "MY_NOTIFICATION"
- CCNotificationCenter::sharedNotificationCenter()->postNotification(MY_NOTIFICATION, (CCObject*)1);
接收通知(添加监听):
方法:
- void addObserver(CCObject *target,
- SEL_CallFuncO selector,
- const char *name,
- CCObject *obj);
例子:
- CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::myNotification), MY_NOTIFICATION, NULL);
- // Handle the notification
- void HelloWorld::myNotification(CCObject* obj)
- {
- CCLOG("Notification achieved. ID: %i", (int)obj);
- }
注意:一般的在接受通知的一方在接受完通知后需要remove监听。
方法:
- void removeObserver(CCObject *target,const char *name);
- int removeAllObservers(CCObject *target);
(注意第二个方法: returns the number of observers removed)
例子:
- HelloWorld::~HelloWorld()
- {
- CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MY_NOTIFICATION);
- // CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this);
- }
0 0
- Cocos2d-x CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x 中 CCNotificationCenter 的使用
- cocos2d-x节点(CCNotificationCenter.h)API
- cocos2d CCNotificationCenter
- cocos2d-x中CCNotificationCenter(消息中心)的应用
- COCOS2D-X之CCNotificationCenter的简单使用Demo
- Cocos2d-x CCNotificationCenter 通知中心 自定义消息事件
- Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
- Cocos2d-x v3.x 的事件派发机制(消息处理机制CCNotificationCenter)
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- CCNotificationCenter
- CCNotificationCenter
- Cocos2D-x游戏开发之二十三:CCNotificationCenter观察者模式(2)-不同层之间事件的发送和接受
- int* p[2] 和 int (*p)[2],int**p的区别
- 【COCOS2DX-LUA 脚本开发之六】利用Lua强转函数解决使用CCNode报错或无法正常使用以及简单介绍 quick-cocos2d-x 与 OpenQuick 两款Lua免费开源框架
- timus 2021. Scarily interesting!(提示)
- list/map中安全删除元素
- Delphi XE6 学习笔记(9)手动填充ListView及ListBox
- Cocos2d-x CCNotificationCenter
- [leetcode]Divide Two Integers
- css 模板
- Struts2整合 Freemarker 实例
- 以插件目录方式从本地安装Eclipse插件
- sqlserve 数据类型详解
- 云服务PHP读取XML问题失败的问题解决方法
- C++标准库---count()&count_if()
- android刷新控件的使用(下拉和上拉刷新)