Cocos2d-x 中 CCNotificationCenter 的使用
2013-05-31 13:31
441 查看
Cocos2d-x 中 CCNotificationCenter 的使用。
作者: 黄新泽 日期: Post in Cocos2d-x,Tags: Cocos2d,Cocos2d-x,Event,Notification,Observer 2012年09月6日发表评论 (1)查看评论
在前端开发中,JS和as3中都有很好的监听机制, 我们使用Event的addEventListener、dispatchEvent即可实现松耦合。 在尝试使用cocos2d-x的时候,我寻找这种事件机制。
由于自己是不愿去再去写一些一定存在的代码, 就想去找网上找了, 第一想法,就是立马去看PureMvc – c++,这儿的观察者模式可以轻易实现松耦合,但是看了看,感觉在c++中使用有些麻烦,就放弃了。 在google中搜索,找到如何一篇文章: Generic
Observer Pattern and Events in C++ – CodeProject 看了后,觉得可以使使用这个思路去做了。
无意中问了下朋友得知,cocos2d-x中是有 CCNotificationCenter 可以使用的,测试了下效果, 很方便,使用方式如下:
01 | //发送事件 |
02 | CCNotificationCenter::sharedNotificationCenter()->postNotification(CLICK_TEST_MSG, (CCObject*)data); |
03 |
04 | //监听事件 |
05 | void GameManager::initListener() |
06 | { |
07 | CCNotificationCenter::sharedNotificationCenter()->addObserver( this , callfuncO_selector(GameManager::onClickTest),CLICK_TEST_MSG,NULL); |
08 | } |
09 |
10 | //处理事件 |
11 | void GameManager::onClickTest(CCObject* obj) |
12 | { |
13 | CCMessageBox( "onClickTest" , "Title" ); |
14 |
15 | //移除监听事件 |
16 | CCNotificationCenter::sharedNotificationCenter()->removeObserver( this , CLICK_TEST_MSG); |
17 | } |
相关文章推荐
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
- COCOS2D-X之CCNotificationCenter的简单使用Demo
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2dx 学习笔记26 CCNotificationCenter的使用
- cocos2d CCNotificationCenter
- cocos2d-x CCNotificationCenter 通知中心
- CCNotificationCenter的使用
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
- cocos2d-x 血泪史(-1) CCNotificationCenter 与LUA
- Cocos2d-x CCNotificationCenter
- Cocos2d-x 2.x编程之CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心 自定义消息事件
- Cocos2d-x v3.x 的事件派发机制(消息处理机制CCNotificationCenter)
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x节点(CCNotificationCenter.h)API
- cocos2d-x CCNotificationCenter一个优化想法
- Cocos2d-x CCNotificationCenter 通知中心