Here we will share the code used to record our learning NotificationCenter. Here we will use icationicationmanager for management and use icationicationcenter.
Icationicationmanager. cpp
# Include "icationicationmanager. h "# include" VisibleRect. h "# define MSG_SWITCH_STATE" SwitchState "USING_NS_CC; enum {kSpriteTag}; void runManager () {auto mSene = Scene: create (); auto mLayer = icationicationmanager: create (); mSene-> addChild (mLayer); ctor: getInstance ()-> runWithScene (mSene);} bool icationicationmanager: init () {if (! Layer: init () {return false;} auto sp = Sprite: create ("Images/t1.png"); sp-> setPosition (VisibleRect: center ()); addChild (sp, 1, kSpriteTag); // first create a Close key auto mCloseItem = MenuItemFont: create ("Close", CC_CALLBACK_1 (icationicationmanager: closeManager, this )); // set the relative coordinate of the close key to mCloseItem-> setPosition (VisibleRect: rightBottom (). x-mCloseItem-> getContentSize (). width, VisibleRect: rightBottom (). y + mCloseItem-> get ContentSize (). height); auto mCloseMenu = Menu: create (mCloseItem, 0); // set Menu to start mCloseMenu-> setPosition (Vec2: ZERO) from () of the coordinate system ); addChild (mCloseMenu);/* create a switchable 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 outcome = MenuItemToggle: createWithCallback (CC_CALLBACK_1 (icationicationmanager: switchToggle, this), mSwitchItem1, mSwitchItem2, NULL); response-> setSelectedIndex (1 ); // switch on: auto mSwitchMenu = Menu: create (mSwitchToggle, NULL); mSwitchMenu-> setPosition (Vec2 (VisibleRect: bottom (). x, VisibleRect: bottom (). y + 100); addChild (mSwitchMenu); setIsConnectToggle (true); // listen Return true;} void icationicationmanager: switchToggle (Ref * sender) {auto obj = (MenuItemToggle *) sender; auto sp1 = (Sprite *) getChildByTag (kSpriteTag ); if (obj-> getSelectedIndex () {// switch onsp1-> setOpacity (255); // transparency 100%} else {// switch offsp1-> setOpacity (127 ); // translucent} void icationicationmanager: setIsConnectToggle (bool B) {if (B) {icationicationcenter: getInstance ()-> addObserver (this, callfuncO_selector (No TificationManager: switchToggle), MSG_SWITCH_STATE, NULL);} else {icationcenter center: getInstance ()-> removeObserver (this, MSG_SWITCH_STATE);} icationicationmanager ::~ Icationicationmanager () {icationicationcenter: getInstance ()-> removeObserver (this, MSG_SWITCH_STATE);} // exit 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}
Icationicationmanager. h
# Ifndef icationicationmanager_h # define notifnotifmanager_h # include "cocos2d. h" class NotificationManager: public cocos2d: Layer {public: virtual ~ Icationicationmanager (); virtual bool init (); void closeManager (cocos2d: Ref * sender); void switchToggle (cocos2d: Ref * sender); void setIsConnectToggle (bool B ); CREATE_FUNC (icationicationmanager); // create an automatic release object}; void runManager (); # endif/* NOTIFICATIONMANAGER_H */