Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
2017-10-06 09:13
477 查看
Cocos2D-X为我们准备了太多东西,当我们想要监听一个动作但是都不能老是盯在这一件事情上的时候就可以用他为我们量身定制的观察者模式,今天我们初步来学习一下这个传说很久的观察这模式在Cocos2D-X中的简单运用。
其实观察者模式的初步学习还是很简单的我们只要定义两个函数即可:
[cpp] view
plain copy
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void sengMsg(CCObject *pSender);
void testMSG(CCObject *pSender);
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
然后在init()函数中添加事件的监听。在SendMSG()函数中发送事件消息。
[cpp] view
plain copy
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(HelloWorld::testMSG),"test",NULL);
CCMenuItemLabel *labelItem = CCMenuItemLabel::create(CCLabelTTF::create("Send MSG","Arial",26),this,menu_selector(HelloWorld::sengMsg));
CCMenu *menu = CCMenu::create(labelItem,NULL);
this->addChild(menu);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::sengMsg(CCObject *pSender)
{
CCLOG("sendMSG");
CCNotificationCenter::sharedNotificationCenter()->postNotification("test",NULL);
}
void HelloWorld::testMSG(CCObject *pSender)
{
CCLOG("testMSG");
}
现在当我们点击菜单按钮式就可以看到事件被发送和接受的调试信息:
顶0
踩
其实观察者模式的初步学习还是很简单的我们只要定义两个函数即可:
[cpp] view
plain copy
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void sengMsg(CCObject *pSender);
void testMSG(CCObject *pSender);
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
然后在init()函数中添加事件的监听。在SendMSG()函数中发送事件消息。
[cpp] view
plain copy
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(HelloWorld::testMSG),"test",NULL);
CCMenuItemLabel *labelItem = CCMenuItemLabel::create(CCLabelTTF::create("Send MSG","Arial",26),this,menu_selector(HelloWorld::sengMsg));
CCMenu *menu = CCMenu::create(labelItem,NULL);
this->addChild(menu);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::sengMsg(CCObject *pSender)
{
CCLOG("sendMSG");
CCNotificationCenter::sharedNotificationCenter()->postNotification("test",NULL);
}
void HelloWorld::testMSG(CCObject *pSender)
{
CCLOG("testMSG");
}
现在当我们点击菜单按钮式就可以看到事件被发送和接受的调试信息:
顶0
踩
相关文章推荐
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- Cocos2D-x游戏开发之二十三:CCNotificationCenter观察者模式(2)-不同层之间事件的发送和接受
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
- 【IPhone开发】NSNotificationCenter观察者模式给主线程发送通知
- cocos2dx3.3 CCNotificationCenter观察者模式
- Cocos2dx 提供的观察者模式工具类 CCNotificationCenter
- 观察者模式——CCNotificationCenter
- Cocos2d-x观察者模式其实很简单!!--之游戏开发《赵云要格斗》(13)
- 关于cocos2dx观察者模式(CCNotificationCenter)
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x CCNotificationCenter一个优化想法
- 【Cocos2d-x游戏开发lua篇之基础】显示文字
- Cocos2d-X游戏开发之物理引擎Box2D基础篇(十五)
- 【Cocos2d-x】开发基础-第一个Cocos2d-x游戏
- Cocos2d-x游戏开发之CCLabelTTF标签详解之Create方法(设置自动换行的标签)和换行符的巧妙使用(十六)(高级篇) (转)
- [iPhone] 观察者模式与游戏开发
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏