Menu in Cocos2d-X
A special way to display menus in a Cocos2d-X is to first create a menu with CCMenu and then create a menu item with CCMenuItem, in fact, the menu displayed in the program is the result of using CCMenu and CCMenuItemFont together, in the Cocos2d-X CCMenu is only a container, you need to use CCMenuItemFont to achieve the creation of the menu
Program example: create a simple menu
Program code:
# Include "Menu. h "CCScene * Menu: scene () {CCScene * scene = CCScene: create (); Menu * layer = Menu: create (); scene-> addChild (layer); return scene;} bool Menu: init () {// initialize the parent class layer CCLayer: init (); // obtain the window size CCSize winSize = CCDirector: shareddire()-> getWinSize (); // create a Menu (the Menu is only a container) CCMenu * menu = CCMenu: create (); // Add Menu addChild (menu); // create a MenuItem. the default position of the Item is in the central CCMenuItemFont * item = CCMenuItemFont :: create ("This is item"); // Add MenuItem to Menu> addChild (item1); return true ;}
Execution result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + s8zQ8sq + fill + zTpsrCvP48L3A + fill = "brush: java;"> # ifndef _ Menu_H __# define _ Menu_H __# include "cocos2d. h "USING_NS_CC; class Menu: public CCLayer {public: bool init (); static CCScene * scene (); void menuHandler (CCObject *); CREATE_FUNC (Menu );}; # endif/_ HELLOWORLD_SCENE_H __
Add the following code to Menu. cpp:
# Include "Menu. h "CCScene * Menu: scene () {CCScene * scene = CCScene: create (); Menu * layer = Menu: create (); scene-> addChild (layer); return scene;} bool Menu: init () {// initialize the parent class layer CCLayer: init (); // obtain the window size CCSize winSize = CCDirector: shareddire()-> getWinSize (); // create a Menu (the Menu is only a container) CCMenu * menu = CCMenu: create (); // Add Menu addChild (menu); // create a MenuItem. the default position of the Item is in the central CCMenuItemFont * item1 = CCMenuItemFont: create ("This is item1"); // Add MenuItem to Menu> addChild (item1); // create a MenuItem, the default Item location is in the central CCMenuItemFont * item2 = CCMenuItemFont: create ("This is item2"); // Add MenuItem menu-> addChild (item2 ); // set item2 location item2-> setPositionY (item1-> getPositionY ()-100); // set the response function: Call Menu :: menuHandler // The first parameter: the object where the response function is located // The second parameter: member function pointer item1-> setTarget (this, menu_selector (Menu: menuHan Dler); item2-> setTarget (this, menu_selector (Menu: menuHandler )); // set the ID of the menu item // the ID of item1 to 1 // the ID of item2 is 2 item1-> setTag (1); item2-> setTag (2 ); return true;} // response Menu event void Menu: menuHandler (CCObject * obj) {// forcibly convert obj to the Menu item type CCMenuItemFont * item = (CCMenuItemFont *) obj; // obtain the ID int tag = item-> getTag () of the menu item. // you can determine which menu the user has clicked if (1 = tag) {MessageBox (NULL, TEXT ("item1 is clicked! "), TEXT (" MenuItem response "), MB_ OK);} else {MessageBox (NULL, TEXT (" item2 is clicked! "), TEXT (" MenuItem response "), MB_ OK );}}
Execution result:
After clicking "This is Item1:
After clicking "This is Item2:
Zookeeper