The game world is colorful, no matter how beautiful the game, multi-play game, in the interaction with the game user is often the menu. In the previous chapter we have probably learned about directors, nodes, layers, elves. This chapter is themed on a menu.
The menu contains the following content:
1. Sprite menu item (menuitemsprite)
2. Picture menu item (menuitemimage)
3. Text menu item (menuitemfont)
4. Label menu item (Menuitemlabel)
4. Switch menu item (menuitemtoggle)
The inheritance relationship of the menu:
Here the Picture menu item is also inherited from the Sprite menu. Picture menu items can also be called Sprite menus. The text menu item inherits from the Label menu item. In general, the menu is divided into three main categories.
Concrete implementation cases:
1#include"HelloWorldScene.h"2 using_ns_cc;3 4scene*Helloworld::createscene ()5 {6Auto scene =scene::create ();7 8Auto Layer =helloworld::create ();9 TenScene->addChild (layer); One A returnscene; - } - the //On "Init" need to initialize your instance - BOOLHelloworld::init () - { - ////////////////////////////// + //1. Super init First - if( !layer::init ()) + { A return false; at } - - //get the screen size from the director Singleton -Size visiblesize = Director::getinstance ()getvisiblesize (); - - //----------------------------------------------------------------------- in //Sprite Menu - toAuto SPR =sprite::create ("Closenormal.png");//Creating Sprites + -Auto Pstartitem =menuitemsprite::create (Spr,spr,cc_callback_1 (Helloworld::menuclosecallback, This)); the *Pstartitem->setposition ( -, -); $ Panax Notoginseng - the //----------------------------------------------------------------------- + //Picture Menu A themenuitemimage* Pcloseitem =Menuitemimage::create ( + "Closenormal.png",//Normal picture - "Closeselected.png",//Click image $Cc_callback_1 (Helloworld::menuclosecallback, This));//Click the menu to perform the function $Pcloseitem->setposition ( $, -); - - the - //-----------------------------------------------------------------------Wuyi //label Menu the -Auto label = Label::createwithsystemfont ("I am Label Item.","Arial", -);//label Wu - //label Menu Aboutmenuitemlabel* Plabelitem = menuitemlabel::create (Label,cc_callback_1 (Helloworld::menuclosecallback, This)); $ -Plabelitem->setposition ( -, -); - - A //----------------------------------------------------------------------- + //Text Menu the -Auto Pfontitem =menuitemfont::create ("Hello", Cc_callback_1 (Helloworld::menuclosecallback, This)); $ thePfontitem->setposition ( $, $); the the the //Switch Menu - //Menuitemtoggle inMenuitemtoggle * Ptogglemenu =Menuitemtoggle::createwithcallback ( theCc_callback_1 (Helloworld::menuclosecallback, This), theMenuitemfont::create (" on"), AboutMenuitemfont::create ("Off"), the NULL); thePtogglemenu->setposition ( $, -); the + - the //-----------------------------------------------------------------------Bayi //menu contains all the menu subkeys themenu* Pmenu =menu::create (Pstartitem,pcloseitem, Plabelitem,pfontitem,ptogglemenu, NULL); the -Pmenu->setposition (Vec2::zero);//location at the origin point - the This-AddChild (pmenu); the the return true; the } - the the voidHelloworld::menuclosecallback (ref*Psender) the {94Log"Click Me Now"); the the}
:
Introduction to common methods of menu class
The menu class provides different ways to arrange menu items, use them to arrange your menu items vertically, or to arrange them horizontally, briefly describing the parameters and usage of these methods:
1. Use void alignitemsvertically (), and void alignitemsverticallywithpadding (float padding) to arrange the menu items in a vertical direction. Where the padding parameter is the distance between each menu item in the vertical direction, and if it does not, it is arranged in the default spacing.
2. Use void alignitemshorizontally (), and void alignitemshorizontallywithpadding (float padding) to arrange the menu items in a horizontal direction. Where the padding parameter is the space between each menu item in the horizontal direction, and if it does not, it is arranged in the default spacing.
Ok. This concludes the sharing of the Cocos2d menu. The next chapter takes Cocos2d's action as the main content to carry on the analysis
Cocos2d Getting Started tutorial four cocos2d-x menu