Comprehensive application of menu in Cocos2d-X

Source: Internet
Author: User

Today, the code and project previously written are integrated into a menu. You can switch to the previous project through the menu.

Project directory of the program


Main code analysis:

Create a menu in lessonmenu. H, traverse the menu, and switch to each project through the menu

# Ifndef _ lessonmenu_h _ # DEFINE _ lessonmenu_h _ # include "cocos2d. H "using_ns_cc; Class lessonmenu: Public cclayer {public: bool Init (); static ccscene * scene (); void enterlasson (ccobject * sender ); // process the touch event bool cctouchbegan (cctouch *, ccevent); void cctouchmoved (cctouch *, ccevent *); create_func (lessonmenu) ;};# endif

Implementation of specific functions in lessonmenu. cpp

# Include "lessonmenu. H "# include" label. H "# include" menuitem. H "# include" textfieldttf. H "# include" appmacros. H "# include" labelatlas. H "# include" scenesecond. H "# include" textfieldttf1.h "# include" bmfont. H "# include" layer. H "# include" Sprite. H "# include" zorder. H "# include" coord. H "# include" sprite1.h "# include" tcceditbox. H "# include" menu. H "# include" menucontinus. H "# include" t14switchcontrol. H "# include" t15slider. H "ccscene * lessonmenu: Scene () {ccscene * s = ccscene: Create (); lessonmenu * layer = lessonmenu: Create (); s-> addchild (layer); Return s;} // Save the created CPP file static const char * lessons [] = {"bmfont. CPP "," coord. CPP "," label. CPP "," labelatlas. CPP "," layer. CPP "," menu. CPP "," menuitem. CPP "," scenesecond. CPP "," Sprite. CPP "," sprite1.cpp "," tcceditbox. CPP "," textfieldttf. CPP "," textfieldttf1.cpp "," zorder. CPP "," menucontinus. CPP "," t14switchcontrol. CPP "," t15slider. CPP "}; bool lessonmenu: Init () {cclayer: Create (); // create menu ccmenu * menu = ccmenu: Create (); addchild (menu ); for (INT I = 0; I <sizeof (lessons)/sizeof (* lessons); I ++) {// create a menu item ccmenuitemfont * Item = ccmenuitemfont :: create (lessons [I]); menu-> addchild (item); // sets the processing function item-> settarget (this, menu_selector (lessonmenu: enterlasson )); // set the ID item-> settag (10000 + I) of the menu item;} // align the menu item menu-> alignitemsvertically (); // settouchenabled (true) of the touch menu item ); settouchmode (outputs); Return true;} void lessonmenu: enterlasson (ccobject * sender) {// forced type conversion ccmenuitem * Item = dynamic_cast <ccmenuitem *> (sender ); // obtain the ID int tag = item-> gettag ()-10000 of the menu item; // call scenario ccscene * scene = NULL; Switch (TAG) {Case 0: {scene = bmfont: Scene ();} break; Case 1: {scene = coord: Scene ();} break; Case 2: {scene = label :: scene ();} break; Case 3: {scene = labelatlas: Scene ();} break; Case 4: {scene = Layer: Scene ();} break; case 5: {scene = menu: Scene ();} break; Case 6: {scene = menuitem: Scene ();} break; Case 7: {scene = scenesecond:: Scene ();} break; case 8: {scene = sprite: Scene ();} break; Case 9: {scene = sprite1: Scene ();} break; case 10: {scene = tcceditbox: Scene ();} break; Case 11: {scene = textfieldttf: Scene ();} break; Case 12: {scene = textfieldttf1:: Scene ();} break; Case 13: {scene = zorder: Scene ();} break; Case 14: {scene = menucontinus: Scene ();} break; case 15: {scene = t14switchcontrol: Scene ();} break; Case 16: {scene = t15slider: Scene ();} break;} If (scene) {ccdirector:: shareddirector ()-> pushscene (scene) ;}// handle the touch event bool lessonmenu: cctouchbegan (cctouch * touch, ccevent) {return true ;} // handle the touch event void lessonmenu: cctouchmoved (cctouch * touch, ccevent *) {// obtain the ccpoint ptdelta = touch-> getdelta (); // The method of sliding up and down only setpositiony (getpositiony () + ptdelta. Y );}

Layerback. h creates a layer with only one return button. Use the return button to implement scenario switching.

#ifndef _LayerBack_H_#define _LayerBack_H_#include "cocos2d.h"USING_NS_CC;class LayerBack : public CCLayer{public:    bool init();    void back(CCObject*);};#endif

The specific implementation code in layerback. cpp

# Include "layerback. H "bool layerback: Init () {cclayer: Init (); // obtain the window size. ccsize winsize = ccdirector: shareddire()-> getwinsize (); ccmenu * menu = ccmenu: Create (); addchild (menu); ccmenuitem * backitem = shortcut: Create ("closenormal.png", "closeselected.png", this, menu_selector (layerback :: back); menu-> addchild (backitem); // set the position of the menu item (set the menu item to the bottom right corner) backitem-> setposition (CCP (winsize. width/2-backitem-> getcontentsize (). width/2, backitem-> getcontentsize (). height/2-winsize. height/2); Return true;} void layerback: Back (ccobject *) {// scenario switch ccdirector: shareddirector ()-> popscene ();}

Execution result:

Zookeeper

Comprehensive application of menu in Cocos2d-X

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.