COCOS2D-X之CCNotificationCenter的简单使用Demo
2014-02-12 16:06
525 查看
一、今天我们要讲的是CCNotificationCenter这个类.这是一个实现观察者模式的类,掌握这个类的用法还是很有用处的.今天这个Demo要实现的效果是通过将触屏消息发送给观察者以使之移动精灵的位置到鼠标点击的地方.代码如下:
本人郑重声明如下
一、本文来自CSDN博客,传送门:http://BlOG.CSDN.NET/yirancpp
二、All Rights Reserved. 任何个人或网站转载本文时不得移除本声明.
三、不得对文章进行修改,除非明确说明.同时欢迎大家评论转载和分享.
#define ObserverName "BOSS"//定义观察者的名字 bool HelloWorld::init() { CCLayer::init(); CCSprite* pBoss = CCSprite::create("Spr.png"); CCSize szWin = CCDirector::sharedDirector()->getWinSize(); pBoss->setPosition(CCPointMake(szWin.width/2,szWin.height/2)); this->addChild(pBoss,0,1000); CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::ObserverCallback),ObserverName, NULL); setTouchEnabled(true); return true; } HelloWorld::~HelloWorld() { CCNotificationCenter::sharedNotificationCenter()->removeObserver(this,"removeObserver");//移除观察者 } void HelloWorld::ObserverCallback(CCObject* pSender) { CCNode* pNode = static_cast<CCNode *>(pSender); switch(pNode->getTag()) { case 100: this->getChildByTag(1000)->setPosition(pNode->getPosition()); } } bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CCPoint pt = pTouch->getLocation(); CCNode* pNode = CCNode::create(); pNode->setPosition(pt); pNode->setTag(100);//可以用Tag来表示不同的消息 CCNotificationCenter::sharedNotificationCenter()->postNotification(ObserverName,pNode); return true; }OK就是这么简单最后附上工程的下载地址http://t.cn/zOAuvxW赶快去下载试试吧
本人郑重声明如下
一、本文来自CSDN博客,传送门:http://BlOG.CSDN.NET/yirancpp
二、All Rights Reserved. 任何个人或网站转载本文时不得移除本声明.
三、不得对文章进行修改,除非明确说明.同时欢迎大家评论转载和分享.
相关文章推荐
- Cocos2d-x 中 CCNotificationCenter 的使用
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
- cocos2dx CCNotificationCenter的简单实用
- Cocos2d-x v3.x 的事件派发机制(消息处理机制CCNotificationCenter)
- COCOS2D-X之使用CURL下载图片的一个简单Demo
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- iOS监听模式系列之NSNotificationCenter的简单使用
- iOS监听模式系列之NSNotificationCenter的简单使用
- cocos2d-x节点(CCNotificationCenter.h)API
- cocos2d CCNotificationCenter
- CCNotificationCenter的使用
- cocos2d-x CCNotificationCenter 通知中心
- Cocos2dx 学习笔记26 CCNotificationCenter的使用
- Cocos2d-x 2.x编程之CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x CCNotificationCenter一个优化想法
- Cocos2d-x CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心