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