Cocos2dx 提供的观察者模式工具类 CCNotificationCenter
2017-08-16 14:46
375 查看
Cocos2dx 提供的观察者模式工具类 CCNotificationCenter
1.addObserver(订阅消息)
2。removeObserver(取消订阅消息)
3.postNotification(发布消息)
同一资源文件的使用
.h:
//发布text消息
void sendMsg(CCObject *pSender);
//接收text消息的回调
void textMsg(CCObject *pSender);
.cpp:
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
MenuItemImage*closeBtn = MenuItemImage::create("CloseSelected.png", "CloseNormal.png",this ,
menu_selector(HelloWorld::sendMsg));
closeBtn->setPosition(Vec2(visibleSize.width*0.1 , visibleSize.height*0.8));
Menu*menu = Menu::create(closeBtn , NULL);
menu->setPosition(Point(0,0));
addChild(menu);
//订阅消息类型为text的消息,不传递数据
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector
(MyHelloWorldScene::textMsg) ,"text" , NULL);
return true;
}
void HelloWorld::sendMsg( CCObject *pSender )
{
CCString*sData = CCString::create("HellWorld");
sData->retain();
CCNotificationCenter::sharedNotificationCenter()->postNotification("text" , sData);
}
void HelloWorld::textMsg( CCObject *pSender )
{
log("订阅成功");
}
不同资源文件夹的使用
HelloWorld.h:
//发布text消息
void sendMsg(CCObject *pSender);
HelloWorld.cpp:
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
MyHelloWorldScene *OtherLayer =MyHelloWorldScene::create(); //接受消息层
scene->addChild(layer);
scene->addChild(OtherLayer);
return scene;
}
bool HelloWorld::init(){
//发送消息
MenuItemImage*closeBtn = MenuItemImage::create("CloseSelected.png", "CloseNormal.png",this ,
menu_selector(HelloWorld::sendMsg));
closeBtn->setPosition(Vec2(visibleSize.width*0.1 , visibleSize.height*0.8));
Menu*menu = Menu::create(closeBtn , NULL);
menu->setPosition(Point(0,0));
addChild(menu);
}
void HelloWorld::sendMsg( CCObject *pSender )
{
CCString*sData = CCString::create("HellWorld");
sData->retain();
CCNotificationCenter::sharedNotificationCenter()->postNotification("text" , sData);
}
MyHelloWorldScene.h:
//接收text消息的回调
void textMsg(CCObject *pSender);
MyHelloWorldScene.cpp:
bool MyHelloWorldScene::init() {
if (!Layer::init()) {
return false;
}
size = Director::getInstance()->getVisibleSize();
//订阅消息类型为text的消息,不传递数据
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector
(MyHelloWorldScene::textMsg) ,"text" , NULL);
return true;
}
void MyHelloWorldScene::textMsg( CCObject *pSender )
{
CCString*pData = (CCString*)pSender;
log("%s" , pData->getCString());
}
1.addObserver(订阅消息)
2。removeObserver(取消订阅消息)
3.postNotification(发布消息)
同一资源文件的使用
.h:
//发布text消息
void sendMsg(CCObject *pSender);
//接收text消息的回调
void textMsg(CCObject *pSender);
.cpp:
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
MenuItemImage*closeBtn = MenuItemImage::create("CloseSelected.png", "CloseNormal.png",this ,
menu_selector(HelloWorld::sendMsg));
closeBtn->setPosition(Vec2(visibleSize.width*0.1 , visibleSize.height*0.8));
Menu*menu = Menu::create(closeBtn , NULL);
menu->setPosition(Point(0,0));
addChild(menu);
//订阅消息类型为text的消息,不传递数据
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector
(MyHelloWorldScene::textMsg) ,"text" , NULL);
return true;
}
void HelloWorld::sendMsg( CCObject *pSender )
{
CCString*sData = CCString::create("HellWorld");
sData->retain();
CCNotificationCenter::sharedNotificationCenter()->postNotification("text" , sData);
}
void HelloWorld::textMsg( CCObject *pSender )
{
log("订阅成功");
}
不同资源文件夹的使用
HelloWorld.h:
//发布text消息
void sendMsg(CCObject *pSender);
HelloWorld.cpp:
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
MyHelloWorldScene *OtherLayer =MyHelloWorldScene::create(); //接受消息层
scene->addChild(layer);
scene->addChild(OtherLayer);
return scene;
}
bool HelloWorld::init(){
//发送消息
MenuItemImage*closeBtn = MenuItemImage::create("CloseSelected.png", "CloseNormal.png",this ,
menu_selector(HelloWorld::sendMsg));
closeBtn->setPosition(Vec2(visibleSize.width*0.1 , visibleSize.height*0.8));
Menu*menu = Menu::create(closeBtn , NULL);
menu->setPosition(Point(0,0));
addChild(menu);
}
void HelloWorld::sendMsg( CCObject *pSender )
{
CCString*sData = CCString::create("HellWorld");
sData->retain();
CCNotificationCenter::sharedNotificationCenter()->postNotification("text" , sData);
}
MyHelloWorldScene.h:
//接收text消息的回调
void textMsg(CCObject *pSender);
MyHelloWorldScene.cpp:
bool MyHelloWorldScene::init() {
if (!Layer::init()) {
return false;
}
size = Director::getInstance()->getVisibleSize();
//订阅消息类型为text的消息,不传递数据
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector
(MyHelloWorldScene::textMsg) ,"text" , NULL);
return true;
}
void MyHelloWorldScene::textMsg( CCObject *pSender )
{
CCString*pData = (CCString*)pSender;
log("%s" , pData->getCString());
}
相关文章推荐
- cocos2dx3.3 CCNotificationCenter观察者模式
- 关于cocos2dx观察者模式(CCNotificationCenter)
- 观察者模式——CCNotificationCenter
- Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- Cocos2D-x游戏开发之二十三:CCNotificationCenter观察者模式(2)-不同层之间事件的发送和接受
- Cocos2dx 学习笔记26 CCNotificationCenter的使用
- cocos2dx CCNotificationCenter的简单实用
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
- cocos2dx 事件响应机制 CCNotificationCenter
- 【IPhone开发】NSNotificationCenter观察者模式给主线程发送通知
- JAVA提供的对观察者模式的支持(观察者模式)
- 观察者模式抽取的工具类
- 多继承下函数指针强制转换所引发的诡异问题(CCNotificationCenter)
- 消息中心和观察者:NSNotificationCenter
- cocos2dx 观察者模式
- CCNotificationCenter处理消息事件
- 一个简单观察者模式的实现 cocos2dx
- cocos-2dx_CCNotificationCenter消…