Instance introduction Cocos2d-x switch menu

Source: Internet
Author: User

The enable menu is implemented by the MenuItemToggle class. It is a menu that can be switched between two States. It can be created through the following functions:

Static MenuItemToggle * createWithCallback (const ccMenuCallback & callback, // The callback function pointer of the menu operation MenuItem * item, // The menu item for switching ...)

The second parameter is an instance object of the MenuItem class. They are menu items displayed in the switch menu. They can be text, images, and sprite menu items, but do not forget the end of NULL.

The following code is a simple text-type switch menu:

auto toggleMenuItem =MenuItemToggle::createWithCallback(                                        CC_CALLBACK_1(HelloWorld::menuItem1Callback,this),                       MenuItemFont::create("On" ),                       MenuItemFont::create("Off"),                       NULL );   Menu* mn = Menu::create(toggleMenuItem,NULL);this->addChild(mn);

Next we will use an example to introduce the use of other complex types of switch menus. This example shows a game sound and background music setting interface, we can enable this function through the switch menu. Our art designer has prepared two pictures for each configuration item (sound effect and background music.

 

Let's take a look at the implementation of the Instance Code. The Code for using the MenuItemSprite menu item in HelloWorldScene. cpp is as follows:

Bool HelloWorld: init () {if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance () -> getVisibleOrigin (); Sprite * bg = Sprite: create ("menu/background.png"); bg-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height/2); this-> addChild (bg); // sound auto soundOnMenuItem = MenuItemImage: create ("menu/on.png", "menu/on.png "); ① auto soundOffMenuItem = MenuItemImage: create ("menu/off.png", "menu/off.png"); ② auto scaling = MenuItemToggle: createWithCallback (CC_CALLBACK_1 (HelloWorld: success, this), soundOnMenuItem, soundOffMenuItem, NULL); ③ soundToggleMenuItem-> setPosition (Director: getInstance ()-> convertToGL (Point (818,220 ))); // music auto musicOnMenuItem = MenuItemImage: create ("menu/on.png", "menu/on.png"); ④ automusicOffMenuItem = MenuItemImage: create ("menu/off.png ", "menu/off.png"); ⑤ auto musicToggleMenuItem = MenuItemToggle: createWithCallback (CC_CALLBACK_1 (HelloWorld: success, this), musicOnMenuItem, musicOffMenuItem, NULL ); ⑥ musicToggleMenuItem-> setPosition (ctor: getInstance ()-> convertToGL (Point (818,362); // OK button autookMenuItem = MenuItemImage: create ("menu/ok-down.png ", "menu/ok-up.png"); okMenuItem-> setPosition (Director: getInstance ()-> convertToGL (Point (600,510); Menu * mn = Menu: create (soundToggleMenuItem, musicToggleMenuItem, okMenuItem, NULL); 7mn-> setPosition (Point: ZERO); this-> addChild (mn); return true ;}

In the above Code, ① Is the image menu item for creating sound effects, and ② is the image menu item for creating sound effects. The ③ line of code is to create the switching menu item MenuItemToggle through the createWithCallback function. Similar to the ④ ~ 6. A background music switch menu item is created. The Code in line 7 creates a Menu object through the switch Menu item created above.

 

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.