Create menu items in Cocos2d-X

Source: Internet
Author: User

Create menu items in Cocos2d-X

Classes for creating menus in Cocos2d-X:

CCMenuItemFont: Create a menu item in plain text

CCMenuItemAtlasFont: Create menu items with art Fonts

CCMenuItemImage: Create a menu item with an image

CCMenuItemLabel: Creates a menu item with a label.

CCMenuItemSprite: Use a wizard to create a menu item

CCMenuItemToggle: Create a menu item that can be switched back and forth, such as ON/OFF


Program instance: Create a plain text menu item

Program code:

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu item that only displays text CCMenuItemFont * itemFont = CCMenuItemFont :: create ("This is Font"); // Add menu item menu-> addChild (itemFont); return true ;}

Execution result:


Program instance: Create a menu item with artistic words

First, add a png Image with artistic words under the project directory.


Add the following code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu CCMenuItemAtlasFont * itemAtlas = CCMenuItemAtlasFont with an artistic Font :: create ("123456", "labelatlasimg.png", 24, 32, '0'); // Add menu item menu-> addChild (itemAtlas); return true ;}


Execution result:



Program instance: Create a menu item with an image

I am using the pictures that come with the program when creating a project.

Program code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu item CCMenuItemImage * itemImage = CCMenuItemImage :: create ("CloseNormal.png", "CloseSelected.png"); // Add menu item menu-> addChild (itemImage); return true ;}

Execution result:



Program instance: use tags to create menu items

Program code:

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu item CCMenuItemLabel * itemLabelTTF = CCMenuItemLabel: create (CCLabelTTF :: create ("This is Label TTF item", "Arial", 36); // Add menu item menu-> addChild (itemLabelTTF); return true ;}


Execution result:


Program example: use tags to create menu items and implement color text

First, add a png image to the Resource folder under the project directory.


Program code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu item with a label and display the color text CCMenuItemLabel * itemLabelAtlas = CCMenuItemLabel:: create (CCLabelAtlas: create ("123456", "labelatlasimg.png", 24, 32, '0'); // Add menu item menu-> addChild (itemLabelAtlas ); return true ;}

Execution result:


Program example: use tags to create menu items and implement color text 2

First, add a font file in fnt format to the Resource folder under the project directory.

Program code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu item CCMenuItemLabel * itemLabelBMFont = CCMenuItemLabel: create (CCLabelBMFont :: create ("This is BMFONT item", "bitmapFontTest. fnt "); // Add menu item menu-> addChild (itemLabelBMFont); return true ;}

Execution result


Program instance: Use the Wizard to create a menu item

The created genie uses the built-in images of the project.

Program code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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); // use the Wizard to create a menu item CCMenuItemSprite * itemSprite = CCMenuItemSprite: create (CCSprite :: create ("CloseNormal.png"), CCSprite: create ("CloseSelected.png"); // Add menu item menu-> addChild (itemSprite); return true ;}

Execution result:



Program instance: Create menu items that can be switched back and forth, such as ON/OFF

Program code

# Include "MenuItem. h "CCScene * MenuItem: scene () {CCScene * scene = CCScene: create (); MenuItem * layer = MenuItem: create (); scene-> addChild (layer); return scene;} bool MenuItem: 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 menu that can be switched back and forth // The option CCMenuItem * subItem1 = CCMenuItemFont: create ("ON"); CCMenuItem * subItem2 = CCMenuItemFont :: create ("OFF"); CCMenuItem * subItem3 = CCMenuItemFont: create ("ON/OFF"); // Add the option CCMenuItemToggle * itemToggle = CCMenuItemToggle to the menu item :: create (subItem1); itemToggle-> addSubItem (subItem2); itemToggle-> addSubItem (subItem3); // Add menu item menu-> addChild (itemToggle ); // automatically alignItemsVertically (); return true ;}


Execution result:

Zookeeper

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.