在这里分享记录自己的学习NotificationCenter时候的代码,这里用NotificationManager进行管理使用NotificationCenter。
NotificationManager.cpp
#include "NotificationManager.h" #include "VisibleRect.h" #define MSG_SWITCH_STATE "SwitchState" USING_NS_CC; enum { kSpriteTag }; void runManager(){ auto mSene = Scene::create(); auto mLayer = NotificationManager::create(); mSene->addChild(mLayer); Director::getInstance()->runWithScene(mSene); } bool NotificationManager::init(){ if (!Layer::init()) { return false; } auto sp = Sprite::create("Images/t1.png"); sp->setPosition(VisibleRect::center()); addChild(sp, 1, kSpriteTag); //首先创建一个关闭键 auto mCloseItem = MenuItemFont::create("Close", CC_CALLBACK_1(NotificationManager::closeManager, this)); //设置关闭键相对坐标 mCloseItem->setPosition(VisibleRect::rightBottom().x - mCloseItem->getContentSize().width, VisibleRect::rightBottom().y + mCloseItem->getContentSize().height); auto mCloseMenu = Menu::create(mCloseItem, 0); //设置Menu从坐标系的(0,0)处開始 mCloseMenu->setPosition(Vec2::ZERO); addChild(mCloseMenu); /*创建一个可开关的Menu*/ auto label1 = LabelTTF::create("switch off", "fonts/Marker Felt.ttf", 26); auto label2 = LabelTTF::create("switch on", "fonts/Marker Felt.ttf", 26); auto mSwitchItem1 = MenuItemLabel::create(label1); auto mSwitchItem2 = MenuItemLabel::create(label2); auto mSwitchToggle = MenuItemToggle::createWithCallback(CC_CALLBACK_1(NotificationManager::switchToggle, this), mSwitchItem1, mSwitchItem2, NULL); mSwitchToggle->setSelectedIndex(1); //switch on选中显示 auto mSwitchMenu = Menu::create(mSwitchToggle, NULL); mSwitchMenu->setPosition(Vec2(VisibleRect::bottom().x, VisibleRect::bottom().y + 100)); addChild(mSwitchMenu); setIsConnectToggle(true); //进行监听 return true; } void NotificationManager::switchToggle(Ref* sender){ auto obj = (MenuItemToggle*)sender; auto sp1 = (Sprite*)getChildByTag(kSpriteTag); if (obj->getSelectedIndex()){ //switch on sp1->setOpacity(255); //透明度100% } else{ //switch off sp1->setOpacity(127); //半透明 } } void NotificationManager::setIsConnectToggle(bool b){ if (b){ NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(NotificationManager::switchToggle), MSG_SWITCH_STATE,NULL); } else{ NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE); } } NotificationManager::~NotificationManager(){ NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE); } //退出 void NotificationManager::closeManager(Ref *sender){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
NotificationManager.h
#ifndef NOTIFICATIONMANAGER_H #define NOTIFICATIONMANAGER_H #include "cocos2d.h" class NotificationManager :public cocos2d::Layer { public: virtual ~NotificationManager(); virtual bool init(); void closeManager(cocos2d::Ref *sender); void switchToggle(cocos2d::Ref *sender); void setIsConnectToggle(bool b); CREATE_FUNC(NotificationManager); //创建一个自己主动释放对象 }; void runManager(); #endif /*NOTIFICATIONMANAGER_H*/