在這裡分享記錄自己的學習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 onsp1->setOpacity(255); //透明度100%}else{ //switch offsp1->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;#endifDirector::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*/