Cocos2d Getting Started tutorial four cocos2d-x menu

Source: Internet
Author: User
Tags addchild

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:

#include "HelloWorldScene.h" USING_NS_CC;        scene* helloworld::createscene () {Auto scene = Scene::create ();    Auto layer = Helloworld::create ();    Scene->addchild (layer); return scene;}  On "Init" need to initialize your Instancebool helloworld::init () {////////////////////////////////1. Super Init first if (!    Layer::init ()) {return false;        }//From the Director Singleton to get the screen size visiblesize = Director::getinstance ()->getvisiblesize (); -----------------------------------------------------------------------//Elf menu Auto Spr =sprite::create ("Close  Normal.png ");        Create the Sprite Auto Pstartitem =menuitemsprite::create (Spr,spr,cc_callback_1 (Helloworld::menuclosecallback, this));                Pstartitem->setposition (100,100); -----------------------------------------------------------------------//Picture menu menuitemimage* Pcloseitem = Menu Itemimage::create ("Closenormal. png ",//Normal picture" closeselected.png ",//click picture Cc_callback_1 (Helloworld::menuclosecallback,this))///Click the menu to perform the function pcloseitem->setposition (200,1               00); -----------------------------------------------------------------------//Label menu Auto label = label::createwiths   Ystemfont ("I am Label Item.", "Arial", 30);  Label//Label menu menuitemlabel* plabelitem = Menuitemlabel::create (Label,cc_callback_1 (Helloworld::menuclosecallback,        This));            Plabelitem->setposition (400,100); -----------------------------------------------------------------------//Text menu auto Pfontitem =menuitemfont::c        reate ("Hello", Cc_callback_1 (Helloworld::menuclosecallback, this));            Pfontitem->setposition (200,200);                                             Switch Menu//menuitemtoggle menuitemtoggle * Ptogglemenu =menuitemtoggle::createwithcallback (                       Cc_callback_1 (Helloworld::menuclosecallback,this), Menuitemfont::create ("on"), MenuItem    Font::create ("Off"), NULL);            Ptogglemenu->setposition (200,300); -----------------------------------------------------------------------//Menu accommodates all menu subkeys menu* pmenu = Menu::create (P        Startitem,pcloseitem, Plabelitem,pfontitem,ptogglemenu, NULL);        Pmenu->setposition (Vec2::zero);//Position at Origin This->addchild (Pmenu); return true;} void Helloworld::menuclosecallback (ref* psender) {log ("click Me");}



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


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2d Getting Started tutorial four cocos2d-x menu

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.