Menu effects in Cocos2d-X

Source: Internet
Author: User

Menu effects in Cocos2d-X

In the Cocos2d-X, menu and action can be combined to implement menu Effects

Program Example 1: use a combination of menus and actions to implement menu effects 1

# 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 CCMenuItemFont * itemFont = CCMenuItemFont :: create ("This is Font"); // Add menu item menu-> addChild (itemFont); // create a menu CCMenuItemAtlasFont * itemAtlas = CCMenuItemAtlasFont with an artistic Font :: create ("123456", "labelatlasimg.png", 24, 32, '0'); // Add menu item menu-> addChild (itemAtlas ); // create a menu item CCMenuItemImage * itemImage = CCMenuItemImage: create ("CloseNormal.png", "CloseSelected.png"); // Add a menu item menu-> addChild (itemImage ); // use a Label to create a menu item CCMenuItemLabel * itemLabelTTF = CCMenuItemLabel: create (CCLabelTTF: create ("This is Label TTF item", "Arial", 36 )); // Add menu item menu-> addChild (itemLabelTTF); // 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 ); // create a menu item CCMenuItemLabel * itemLabelBMFont = CCMenuItemLabel: create (CCLabelBMFont: create ("This is BMFONT item", "bitmapFontTest. fnt "); // Add menu item menu-> addChild (itemLabelBMFont); // create menu item CCMenuItemSprite * itemSprite = CCMenuItemSprite: create (CCSprite :: create ("CloseNormal.png"), CCSprite: create ("CloseSelected.png"); // Add menu item menu-> addChild (itemSprite ); // 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 align the menu item menu-> alignItemsVertically (); // implement the menu Appearance Effect // enter the first menu from the left of the window, and the second from the right, and so on // create an array to save the menu item CCArray * arr = menu-> getChildren (); CCObject * obj; // used to traverse the menu item int index = 0; CCARRAY_FOREACH (arr, obj) {// force type conversion (convert the menu item type to the same type) CCMenuItem * item = (CCMenuItem *) obj; if (index % 2 = 0) {// set the position of the menu item (set the menu to the leftmost of the window) item-> setPositionX (item-> getPositionX ()-winSize. width); // move the menu (set the menu to move from left to right) // The first parameter: time when the menu is moved // The second parameter: position after menu movement CCMoveBy * moveBy = CCMoveBy: create (10, ccp (winSize. width, 0); item-> runAction (moveBy);} else {// move it to the right item-> setPositionX (item-> getPositionX () + winSize. width); // move the menu (set the menu to move from right to left) // The first parameter: time when the menu is moved // The second parameter: position after menu movement CCMoveBy * moveBy = CCMoveBy: create (10, ccp (-winSize. width, 0); item-> runAction (moveBy);} index ++;} return true ;}


Execution result:



Program Example 2: Use a combination of menus and actions to implement menu Effects 2

# 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 CCMenuItemFont * itemFont = CCMenuItemFont :: create ("This is Font"); // Add menu item menu-> addChild (itemFont); // create a menu CCMenuItemAtlasFont * itemAtlas = CCMenuItemAtlasFont with an artistic Font :: create ("123456", "labelatlasimg.png", 24, 32, '0'); // Add menu item menu-> addChild (itemAtlas ); // create a menu item CCMenuItemImage * itemImage = CCMenuItemImage: create ("CloseNormal.png", "CloseSelected.png"); // Add a menu item menu-> addChild (itemImage ); // use a Label to create a menu item CCMenuItemLabel * itemLabelTTF = CCMenuItemLabel: create (CCLabelTTF: create ("This is Label TTF item", "Arial", 36 )); // Add menu item menu-> addChild (itemLabelTTF); // 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 ); // create a menu item CCMenuItemLabel * itemLabelBMFont = CCMenuItemLabel: create (CCLabelBMFont: create ("This is BMFONT item", "bitmapFontTest. fnt "); // Add menu item menu-> addChild (itemLabelBMFont); // create menu item CCMenuItemSprite * itemSprite = CCMenuItemSprite: create (CCSprite :: create ("CloseNormal.png"), CCSprite: create ("CloseSelected.png"); // Add menu item menu-> addChild (itemSprite ); // 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 align the menu item menu-> alignItemsVertically (); // implement the menu Appearance Effect // enter the first menu from the left of the window, and the second from the right, and so on // create an array to save the menu item CCArray * arr = menu-> getChildren (); CCObject * obj; float dt = 2.0f; // for delayed display menu // For traversing menu item int index = 0; CCARRAY_FOREACH (arr, obj) {// force type conversion (convert the menu item type to the same type) CCMenuItem * item = (CCMenuItem *) obj; // create a delay action CCDelayTime * delay = CCDelayTime:: create (dt * index); // used to create the special effect CCEaseElasticOut * out = NULL; if (index % 2 = 0) {// set the position of the menu item (set the menu to the leftmost of the window) item-> setPositionX (item-> getPositionX ()-winSize. width); // move the menu (set the menu to move from left to right) // The first parameter: time when the menu is moved // The second parameter: position after menu movement CCMoveBy * moveBy = CCMoveBy: create (10, ccp (winSize. width, 0); // auto effect out = CCEaseElasticOut: create (moveBy);} else {// move to the right item-> setPositionX (item-> getPositionX () + winSize. width); // move the menu (set the menu to move from right to left) // The first parameter: time when the menu is moved // The second parameter: position after menu movement CCMoveBy * moveBy = CCMoveBy: create (10, ccp (-winSize. width, 0); // auto effect out = CCEaseElasticOut: create (moveBy);} // enables the menu item to execute CCSequence * seq = CCSequence :: create (delay, out, NULL); item-> runAction (seq); index ++; // traverse menu item} 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.