Cocos2d-x 3.2 Monopoly Game project Development-Part III menu scene

Source: Internet
Author: User
Tags addchild


The previous section introduced to the title animation introduction into the menu scene, Scene effect




Menuscene . h The header files are as follows:


Class Menuscene:public Layercolor  {public:    static scene* createscene ();      virtual BOOL init ();             Create_func (menuscene);p rivate:size visiblesize; Window size labelttf* Settingsgamettf; Scene text void Menutouchdown (object* psender,control::eventtype event);//Menu click callback method void Addbackgroundsprite (); Add scene background method void Addmenusprites ();//Add Menu method}; #endif


Menuscene . cpp Some of the code is as follows:



BOOL Menuscene::init () {if (! Layercolor::initwithcolor (color4b (255, 255, 255, 255)))    {        return false;    } Visiblesize = Director::getinstance ()->getvisiblesize ();                VEC2 origin = Director::getinstance ()->getvisibleorigin (); Addbackgroundsprite ();//Add Scene Background Method Addmenusprites (); Add the Menu method    return true;}


void Menuscene::addbackgroundsprite () {                 //Add tycoon background image, center display sprite* menuspritelogo= sprite::create (Menu_logo); Menuspritelogo->setposition (CCP (visiblesize.width/2,visiblesize.height)); Menuspritelogo->setanchorpoint ( CCP (0.5,1)); Menuspritelogo->setscale (0.6f); AddChild (Menuspritelogo)             ; Add left Rainbow picture sprite* rainbowsprite= sprite::create (RAINBOW); Rainbowsprite->setposition (CCP (5, visiblesize.height-20)); Rainbowsprite->setanchorpoint (CCP (0,1)); Rainbowsprite->setscale (0.3f); AddChild ( Rainbowsprite);               Let the rainbow picture move around moveby* Rainbowmove = Moveby::create (1,CCP (8,0)); moveby* rainbowmovereverse = Rainbowmove->reverse (); sequence* rainbowaction = sequence::create (rainbowmove,rainbowmovereverse,null); Rainbowsprite->runaction ( Repeatforever::create (Rainbowaction));}


void Menuscene:: Addmenusprites () {//Add stand-alone game MENU scale9sprite* btnnormal = scale9sprite::create (Normal_menu); Set menu normal picture scale9sprite* btnpress = Scale9sprite::create (Press_menu);//set menu Press picture labelttf* Singlegamettf = Labelttf::create (Single_game, font_menu,btn_fontsize);//The Label object required to create the menu controlbutton* singlegamebtn = ControlButton :: Create (Singlegamettf,btnnormal);//Creating Controlbuttonsinglegamebtn->setbackgroundspriteforstate (BtnPress, control::state::selected)///Add Singlebutton menu Singlegamebtn->setposition (CCP (VISIBLESIZE.WIDTH/2, visiblesize.height-200));//Set Position singlegamebtn->setpreferredsize (Size (btn_width,btn_height));// Set Size singlegamebtn->addtargetwithactionforcontrolevents (This,cccontrol_selector (MenuScene::menuTouchDown), Control::eventtype::touch_down)///Set Click Callback method Singlegamebtn->settag (Btn_single_game_tag);//Set Tagaddchild (               SINGLEGAMEBTN)///Add the menu//Other menu Add method, similar to the same as above, no longer repeat ..... ..... ..... Settings menu is not the same, the menu click has the effect of turning on or off. scale9sprite*BtnNormal3 = Scale9sprite::create (Normal_menu); scale9sprite* BTNPRESS3 = scale9sprite::create (press_menu); bool music_on = Userdefault::getinstance () Getboolforkey (music_on_key,true);//Get Sound settings labelttf* settingsgamettf;//If the sound is on, the menu display on, if off, show Offif (music_on) { Settingsgamettf = Labelttf::create (music_on,font_menu,btn_fontsize);} else{Settingsgamettf = labelttf::create (music_off,font_menu,btn_fontsize);} controlbutton* settingsgamebtn = controlbutton::create (SETTINGSGAMETTF,BTNNORMAL3);settingsGameBtn-> Setbackgroundspriteforstate (BTNPRESS3, control::state::selected); Settingsgamebtn->setposition (CCP ( visiblesize.width/2,visiblesize.height-320)); Settingsgamebtn->setpreferredsize (Size (Btn_Width,Btn_Height)); Settingsgamebtn->addtargetwithactionforcontrolevents (This,cccontrol_selector (MenuScene:: MenuTouchDown), Control::eventtype::touch_down);//Add Setting callback Settingsgamebtn->settag (Btn_music_tag); AddChild (                SETTINGSGAMEBTN); ...........................}


Menu click on the callback method, according to click on the object tag call the appropriate method, here the main case statement needs to add {} parentheses, or compile an error


void Menuscene:: Menutouchdown (object* psender,control::eventtype event) {log ("single Touched"); controlbutton* button = (controlbutton*) psender;int tag = Button->gettag (); switch (tag) {case btn_single_game_tag:{ Log ("single game");//director::getinstance ()->pushscene (Mapchoosescene::createscene ());} Case Btn_multi_game_tag:{log ("Multi Game"); Case Btn_music_tag:{bool music_on = userdefault::getinstance ()->getboolforkey (music_on_key,true); if (music_on) { Userdefault::getinstance ()->setboolforkey (music_on_key,false);//Set the Sound off Button->settitleforstate (MUSIC_OFF, Control::state::normal);//Let menu text display Off}else{userdefault::getinstance ()->setboolforkey (music_on_key,true);// Set the sound effect to open button->settitleforstate (music_on,control::state::normal);//Let menu text display on}break;} Case Btn_quit_game_tag:{log ("Quit Game");//popuplayer (); Popup exit dialog box, code more, subsequent write break;}   Default: Break     ;}

The code is relatively simple


Not to be continued ........


Cocos2d-x 3.2 Monopoly Game project Development-Part III menu scene

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.