Instance introduction Cocos2d-x genie menu and picture menu

Source: Internet
Author: User

The genie menu class is MenuItemSprite, and the image menu class is MenuItemImage. Since MenuItemImage inherits from MenuItemSprite, the image menu also belongs to the genie menu. Why is it called the genie menu? This is because these menu items have the characteristics of the genie, we can make the genie move up, the specific use is to put a genie in the menu as a menu item.

MenuItemSprite in The Wizard menu class. One of its creation functions is defined as follows:

Static MenuItemSprite * create (Node * normalSprite, // The sprite Node * selectedSprite when the menu item is normally displayed, // The sprite Node * disabledSprite when the menu item is selected, // const ccMenuCallback & callback // callback function pointer of the menu operation when the menu item is disabled)

It is troublesome to use MenuItemSprite. Before creating MenuItemSprite, you must first create the genie (normalSprite, selectedSprite, and disabledSprite) in three states ). MenuItemSprite also has some create functions, which can omit the disabledSprite parameter.

If the genie is composed of images, we can use the image menu MenuItemImage to achieve the same effect as the genie menu. One of the create functions of the MenuItemImage class is defined as follows:

Static MenuItemImage * create (const std: string & normalImage, // The image const std: string & selectedImage when the menu item is normally displayed, // select the image const std: string & disabledImage when the menu item is disabled, // The callback function pointer of the image const ccMenuCallback & callback // menu operation when the menu item is disabled)

MenuItemImage also has some create functions, which can omit the disabledImage parameter.

An example is provided to describe how to use the wizard menu and Image menu, as shown in.


The init function 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); // start Sprite * startSpriteNormal = Sprite: create ("menu/start-up.png "); 1 Sprite * startSpriteSelected = Sprite: create ("menu/start-down.png"); 2 MenuItemSprite * startMenuItem = MenuItemSprite: create (example, startSpriteSelected, CC_CALLBACK_1 (HelloWorld: example, this); ③ startMenuItem-> setPosition (Director: getInstance ()-> convertToGL (Point (700,170); ④ // set the image menu MenuItemImage * settingMenuItem = MenuItemImage :: create ("menu/setting-up.png", "menu/setting-down.png", CC_CALLBACK_1 (HelloWorld: menuItemSettingCallback, this); ⑤ settingMenuItem-> setPosition (Director: getInstance () -> convertToGL (Point (480,400); ⑥ // help Image menu MenuItemImage * helpMenuItem = MenuItemImage: create ("menu/help-up.png", "menu/help-down.png ", CC_CALLBACK_1 (HelloWorld: menuItemHelpCallback, this); 7helpmenuitem-> setPosition (Director: getInstance ()-> convertToGL (Point (860,480 ))); export Menu * mu = Menu: create (startMenuItem, settingMenuItem, helpMenuItem, NULL); Limit mu-> setPosition (Point: ZERO); limit this-> addChild (mu ); return true ;}

In the above Code ~ (2) The line is to create two genie in different States. (3) The code is to create the MenuItemSprite object in the Wizard menu. (4) The code is to set the position of the Start menu item (startMenuItem, note that the coordinates are (700,170). Since the coordinates of (700,170) are UI coordinates, You need to convert them to OpenGL coordinates.

Lines 5 and 7 are used to create the MenuItemImage object in the Image menu. lines 6 and 7 are used to set the image menu position. The code in the nth line is the Menu object, and the code in the nth line is the Menu position mu-> setPosition (Point: ZERO). The set position is Point: ZERO, it is equivalent to Point (0.0f, 0.0f ).

In addition, because the size of the background image is 1136x640, and the default window size of Win32 is 960x640, We need to reset the size. Modify the AppDelegate. cpp Code 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); // start Sprite * startSpriteNormal = Sprite: create ("menu/start-up.png "); 1 Sprite * startSpriteSelected = Sprite: create ("menu/start-down.png"); 2 MenuItemSprite * startMenuItem = MenuItemSprite: create (example, startSpriteSelected, CC_CALLBACK_1 (HelloWorld: example, this); ③ startMenuItem-> setPosition (Director: getInstance ()-> convertToGL (Point (700,170); ④ // set the image menu MenuItemImage * settingMenuItem = MenuItemImage :: create ("menu/setting-up.png", "menu/setting-down.png", CC_CALLBACK_1 (HelloWorld: menuItemSettingCallback, this); ⑤ settingMenuItem-> setPosition (Director: getInstance () -> convertToGL (Point (480,400); ⑥ // help Image menu MenuItemImage * helpMenuItem = MenuItemImage: create ("menu/help-up.png", "menu/help-down.png ", CC_CALLBACK_1 (HelloWorld: menuItemHelpCallback, this); 7helpmenuitem-> setPosition (Director: getInstance ()-> convertToGL (Point (860,480 ))); export Menu * mu = Menu: create (startMenuItem, settingMenuItem, helpMenuItem, NULL); Limit mu-> setPosition (Point: ZERO); limit this-> addChild (mu ); return true ;}

In the above Code ~ (2) The line is to create two genie in different States. (3) The code is to create the MenuItemSprite object in the Wizard menu. (4) The code is to set the position of the Start menu item (startMenuItem, note that the coordinates are (700,170). Since the coordinates of (700,170) are UI coordinates, You need to convert them to OpenGL coordinates.

Lines 5 and 7 are used to create the MenuItemImage object in the Image menu. lines 6 and 7 are used to set the image menu position. The code in the nth line is the Menu object, and the code in the nth line is the Menu position mu-> setPosition (Point: ZERO). The set position is Point: ZERO, it is equivalent to Point (0.0f, 0.0f ).

In addition, because the size of the background image is 1136x640, and the default window size of Win32 is 960x640, We need to reset the size. Modify the AppDelegate. cpp Code as follows:

boolAppDelegate::applicationDidFinishLaunching() {   // initialize director   auto director = Director::getInstance();   auto glview = director->getOpenGLView();   if(!glview) {       glview = GLView::create("My Game");             glview->setFrameSize(1136, 640);                                                                              ①       director->setOpenGLView(glview);   } … …}
 

We need to add the glview-> setFrameSize (1136,640) Code in line ①.

Related Article

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.