Cocos2d-x-3.1 icationicationcenter (coco2d-x Study Notes 8)

Source: Internet
Author: User

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 */



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.