Comprehensive application of Menu in Cocos2d-X
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
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/uxL88L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;" ># 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 (kCCTouchesOneByOne); return true;} void LessonMenu: EnterLasson (CCObject * sender) {// force type conversion CCMenuItem * item = dynamic_cast
(Sender); // obtain the ID int tag of the menu item = item-> getTag ()-10000; // 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: shareddire()-> 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 (); // only the method of moving up and down 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