Cocos2d-x 3.2 Monopoly Game project development-part fourth exit the dialog box

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > This part of the code from the Internet, the basic is written in the previous version, you need to change a little bit, effects </span>


First look at the void Menuscene of how the dialog box is called in MenuScene.cpp::p Opuplayer () {    //define a pop-up layer, pass in a background graph    popuplayer* Popdialog = Popuplayer::create (DIALOG_BG);    Contentsize is an optional setting that can not be set if set to scale it as a 9 figure    Popdialog->setcontentsize (Ccsizemake (Quit_dialog_size_width, Quit _dialog_size_height));     Popdialog->settitle (dialog_title);    Popdialog->setcontenttext (dialog_content, +,-);    Set the callback function, callback returns a ccnode to get the tag to determine the clicked button    Popdialog->setcallbackfunc (this, Callfuncn_selector (menuscene:: Quitbuttoncallback));    Add button, set picture, text, tag info    popdialog->addbutton (button_bg1, Button_bg3, OK, Btn_quit_ok_tag);    Popdialog->addbutton (Button_bg2, Button_bg3, CANCEL, Btn_quit_cancel_tag);    This->addchild (Popdialog);//Add to current layer}

PopupLayer.h and PopupLayer.cpp are the code that pops up the dialog box:

First Look at PopupLayer.h
const int pop_fontsize = 20;//defines the font size class Popuplayer:p ublic layer{static popuplayer * Create (const char* backgroundimage) ;//Create object void Settitle (const char* title, int fontsize=pop_fontsize) based on background diagram;//Set Dialog title void Setcontenttext (const char* text, int fontsize=pop_fontsize, int padding=50, int paddinttop=100);//Set Dialog text contents void Setcallbackfunc (object* target, Sel_ CALLFUNCN callfun);//Set Key callback method bool AddButton (const char* normalimage, const char* selectedimage, const char* title, int tag =0)///Add Dialog button, such as confirm cancel virtual void onEnter ();//Call virtual void OnExit () when entering,//void Buttoncallback (ccobject* psender)    ; int m_contentpadding;//The white space on both sides of the text content from int m_contentpaddingtop;  The text above the blank area distance ccobject* M_callbacklistener; SEL_CALLFUNCN m_callback;//defines variables with RETAIN attributes    Cc_synthesize_retain (menu*, M__pmenu, Menubutton); Cc_synthesize_retain (sprite*, M__sfbackground, Spritebackground), ...............};


The PopupLayer.cpp section code is as follows:


Popuplayer::P opuplayer (): 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 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) {//menuitem According to the transferred object, the method's callback    m _callbacklistener = target;    M_callback = Callfun;    }


BOOL Popuplayer::init () {             //Initializes the required menu, followed by a follow-up parameter to the menu to add the Item option        menu* menu = Menu::create ();        Menu->setposition (Ccpointzero);        Setmenubutton (menu); Settouchmode (Touch::D ispatchmode::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);//Mask Lower event response return true;}


According to parameters, title, tag, picture effect, add MenuItem option bool Popuplayer::addbutton (const char *normalimage, const char *selectedimage, const char *title, int tag) {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 a text description to MenuItem and set the position in MenuItem size Imenu = Menuimage->getcontentsize ();    labelttf* TTF = labelttf::create (title, "", +);    Ttf->setcolor (ccc3 (0, 0, 0));    Ttf->setposition (CCP (IMENU.WIDTH/2, IMENU.HEIGHT/2));    Menuimage->addchild (TTF);    Getmenubutton ()->addchild (menuimage);    return true;

Now look at MenuItem's callback method Buttoncallback


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 invokes the Quitbuttoncallback () method of the Menuscene object passed in by the Setcallbackfunc () method    }    this->removefromparent ();// Remove the dialog box from the parent node}

The dialog box pops up before invoking the OnEnter method for interface initialization work

void Popuplayer::onenter () {...    ...... ........ Size contentsize;    Set the dialog background, code omit    //Add button, and set its position    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 + +;}    Show dialog title contents omit    //Add dialog Pop-up effect    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 to write a little more, for the sake of testing, I upload the code, as follows:

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

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



Cocos2d-x 3.2 Monopoly Game project development-part fourth exit the dialog box

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.