Cocos2d-x 3.2 Monopoly game project development-Part 4 exit dialog box, cocos2d-x Project Development

Source: Internet
Author: User
Tags addchild

Cocos2d-x 3.2 Monopoly game project development-Part 4 exit dialog box, cocos2d-x Project Development

<Span style = "font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255);"> I checked this part of the code online, it is basically written in previous versions. You only need to change it a little. </span>


First look at the MenuScene. cpp: popupLayer () {// define a pop-up layer and input a background image PopupLayer * popDialog = PopupLayer: create (DIALOG_BG ); // ContentSize is an optional setting and can be left unspecified. If it is set as a 9-figure zoom popDialog-> setContentSize (CCSizeMake (Quit_Dialog_Size_Width, Quit_Dialog_Size_Height )); popDialog-> setTitle (DIALOG_TITLE); popDialog-> setContentText (DIALOG_CONTENT, 20, 60,250); // set the callback function, callback returns a CCNode to get the popDialog button clicked for tag judgment-> setCallbackFunc (this, callfuncN_selector (MenuScene: quitButtonCallback); // Add button, set the image, text, tag Information popDialog-> addButton (BUTTON_BG1, BUTTON_BG3, OK, success); popdig1-> addButton (success, BUTTON_BG3, CANCEL, success); this-> addChild (popDialog ); // Add to current layer}

PopupLayer. h and PopupLayer. cpp are the code in the pop-up dialog box:

First look at PopupLayer. h
Const int Pop_FontSize = 20; // defines the font size class PopupLayer: public Layer {static PopupLayer * create (const char * backgroundImage ); // create the object void setTitle (const char * title, int fontsize = Pop_FontSize) based on the background image; // set the dialog box title void setContentText (const char * text, int fontsize = Pop_FontSize, int padding = 50, int paddintTop = 100); // set the dialog box text content void setCallbackFunc (Object * target, SEL_CallFuncN callfun); // set the callback method bool addButton (c Onst char * normalImage, const char * selectedImage, const char * title, int tag = 0); // press the button in the Add dialog box. If you are sure to cancel virtual void onEnter (); // call virtual void onExit () when entering; // void buttonCallback (CCObject * pSender); int m_contentPadding; // the gap between the two sides of the text content and int m_contentPaddingTop; // The blank area above the text is located at CCObject * m_callbackListener; SEL_CallFuncN m_callback; // defines the variable CC_SYNTHESIZE_RETAIN (Menu *, m _ pMenu, MenuButton) with the retain attribute _ RETAIN (Sprite *, m _ sfBackGround, SpriteBackGround );.........................};


The code for PopupLayer. cpp is as follows:


PopupLayer: PopupLayer (): m _ pMenu (NULL), m_contentPadding (0), m_contentPaddingTop (0), m_callbackListener (NULL), m_callback (NULL ), m _ sfBackGround (NULL), m _ s9BackGround (NULL), m _ ltContentText (NULL), m _ ltTitle (NULL) {}// release the variable PopupLayer :: ~ PopupLayer () {CC_SAFE_RELEASE (m _ pMenu); CC_SAFE_RELEASE (m _ sfBackGround); CC_SAFE_RELEASE (m _ ltContentText); CC_SAFE_RELEASE (m _ ltTitle ); CC_SAFE_RELEASE (m _ s9BackGround );}


Void PopupLayer: setCallbackFunc (cocos2d: Object * target, SEL_CallFuncN callfun) {// The method callback m_callbackListener = target; m_callback = callfun ;}


Bool PopupLayer: init () {// initialize the required Menu, and then follow up the parameters to add the Item option menu * Menu = menu: create () to the Menu (); menu-> setPosition (CCPointZero); setMenuButton (menu); setTouchMode (Touch: DispatchMode: ONE_BY_ONE); auto listener = EventListenerTouchOneByOne: create (); listener-> setSwallowTouches (true); listener-> onTouchBegan = [] (Touch * t, Event * e) {CCLog ("PopupLayer touch"); return true ;}; _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this); // shield the lower-layer Event Response return true ;}


Add the MenuItem option bool PopupLayer: addButton (const char * normalImage, const char * selectedImage, const char * title, int tag) based on the parameter, title, tag, and image effect) {Size winSize = CCDirector: getInstance ()-> getWinSize (); Point pCenter = ccp (winSize. width/2, winSize. height/2); // create the MenuItem button and set the callback method MenuItemImage * menuImage = MenuItemImage: create (normalImage, selectedImage, this, menu_selector (PopupLayer: buttonCallback )); menuImage-> setTag (tag); menuImage-> setPosition (pCenter); // Add text instructions to MenuItem and set the position Size imenu = menuImage-> getContentSize () in MenuItem (); labelTTF * ttf = LabelTTF: create (title, "", 20); ttf-> setColor (ccc3 (0, 0, 0 )); ttf-> setPosition (ccp (imenu. width/2, imenu. height/2); menuImage-> addChild (ttf); getMenuButton ()-> addChild (menuImage); return true;

Now let's take a look at the callback method buttonCallback of MenuItem.


Void PopupLayer: buttonCallback (cocos2d: CCObject * pSender) {Node * node = dynamic_cast <Node *> (pSender); CCLog ("touch tag: % d ", node-> getTag (); if (m_callback & m_callbackListener) {(m_callbackListener-> * m_callback) (node); // This calls setCallbackFunc () method passed in the MenuScene object's quitButtonCallback () method} this-> removeFromParent (); // remove the dialog box from the parent node}

Call the onEnter method to initialize the interface before the dialog box is displayed.

Void PopupLayer: onEnter (){....................... Size contentSize; // set the background of the dialog box. The code is omitted. // Add the button and set its location this-> addChild (getMenuButton (); float btnWidth = contentSize. width/(getMenuButton ()-> getChildrenCount () + 1); Vector <Node *> vecArray = getMenuButton ()-> getChildren (); int j = 0; for (auto it = vecArray. begin (); it! = VecArray. end (); it ++) {Node * node = dynamic_cast <Node *> (* it); node-> setPosition (Point (winSize. width/2-contentSize. width/2 + btnWidth * (j + 1), winSize. height/2-contentSize.height/3); j ++;} // the title of the displayed dialog box is omitted. // The Action * popupLayer = Sequence: create (ScaleTo :: create (0.0, 0.0), ScaleTo: create (0.15, 1.05), ScaleTo: create (0.08, 0.95), ScaleTo: create (0.08, 1.0), NULL ); this-> runAction (popupLayer );}

Code is written a little more. To facilitate testing, I upload the Code as follows:

Http://download.csdn.net/detail/lideguo1979/8263669

To be continued .................................



Related Article

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.