Definition of UI buttons in Cocos2d-x

Source: Internet
Author: User
Tags addchild

1. You must have two images in different States.

2. Define a menuitemsprite instance and put the sprite instances of the two graphs into the instances of menuitemsprite.

3. Put the menuitemsprite instance into the menu instance

(4. In the pause button, you need to put the menuitemsprite instance into the menuitemtoggle instance, and then the menuitemtoggle instance into the menu instance)

5. addchild the menu instance to the Layer

 

Example

Start game button

1     auto startNormal = Sprite::create("startNormal.png");2     auto startSelected = Sprite::create("startSelected.png");3     auto startSpriteItem = MenuItemSprite::create(startNormal, startSelected, CC_CALLBACK_1(StartUp::menuStartCallback, this));4     auto menu = Menu::create(startSpriteItem, NULL);5     this->addChild(menu);6     menu->setPosition(visibleSize.width / 2, visibleSize.height / 2);

 

1 void StartUp::menuStartCallback(Ref* pSender)2 {3     //Director::getInstance()->getEventDispatcher()->removeEventListener(touchListener);4     Director::getInstance()->replaceScene(TransitionFade::create(1, GameScene::createScene(1)));5 }

 

Pause button

1 // Add the pause button 2 Auto pause_sp1 = sprite: Create ("game_pause_nor.png"); 3 Auto pause_sp2 = sprite: Create ("game_pause_pressed.png"); 4 auto resume_sp1 = sprite:: Create ("game_resume_nor.png"); 5 auto resume_sp2 = sprite: Create ("game_resume_pressed.png"); 6 7 auto pauseitem = menuitemsprite: Create (pause_sp1, pause_sp2 ); 8 auto resumeitem = menuitemsprite: Create (resume_sp1, resume_sp2); 9 auto pausebutton = parameters: createwithtarget (this, parameters (gamescene: parameters), pauseitem, resumeitem, nullptr ); 10 pausebutton-> setanchorpoint (vec2 (0, 0); 11 pausebutton-> setposition (vec2 (size. width/2-pausebutton-> getcontentsize (). width, size. height/2-pausebutton-> getcontentsize (). height); // at the top left corner of the page, 12 13 auto menu = menu: Create (pausebutton, nullptr); 14 menu-> setscale (0.2); 15 addchild (menu, 4, pause_menu );
1 void GameScene::pauseButtonCallBack(Ref* pSender)2 {3     //log("press pause button!");4 }

 

Definition of UI buttons in Cocos2d-x

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.