cocos3.x 扫雷04 菜单项的添加

扫雷04 菜单项的添加

添加的菜单并放置在 _bgMenu上,这里是为了方便在以后添加计分或者计时label的时候可以直接加在_bgmenu上或者直接移位。

四个菜单项添加与回调函数代码

 1: ////////////////////////////////////////////////////////////////////////
 2: // 添加菜单项
 3:
 4: void GameScene::addMenu()
 5: {
 6:     //创建四个菜单项
 7:     _dig = CCMenuItemImage::create("dig_button.png",
 8:                                    "digs_button.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 9:                                    "dign_button.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 10:                                    this,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 11:                                    menu_selector(GameScene::MenuCallDig));
 12:     _flag = CCMenuItemImage::create("flag_button.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 13:                                     "flags_button.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 14:                                     "flagn_button.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 15:                                     this,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 16:                                     menu_selector(GameScene::MenuCallFlag));
 17:     _exitGame = CCMenuItemImage::create("mainmenu2.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 18:                                         "mainmenu2_s.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 19:                                         this,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 20:                                         menu_selector(GameScene::MenuCallExit));
 21:     _restart = CCMenuItemImage::create("restart.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 22:                                        "restarts.png",monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 23:                                        this,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 24:                                        menu_selector(GameScene::MenuCallRestart));
 25:     //用四个菜单项初始化四个菜单
 26:     Menu *menu = Menu::create(_dig,_flag,_exitGame,_restart,NULL);
 27:     //菜单项添加到_bgmenu上去会比较好还是直接添加到当前对象上,这个问题以后再说
 28:     _bgmenu->addChild(menu);
 29:     menu->setPosition(Vec2::ZERO);
 30:
 31:     float lowWidth = _bgmenu->getContentSize().width;
 32:     float lowHeight = _bgmenu->getContentSize().height;
 33:     //设置菜单项的大小,宽度适应为窗口宽度的1/3
 34:     _dig->setScale(lowWidth / 3.0f / _dig->getContentSize().width);
 35:     _flag->setScale(lowWidth / 3.0f / _dig->getContentSize().width);
 36:     _exitGame->setScale(lowWidth / 3.0f / _dig->getContentSize().width);
 37:     _restart->setScale(lowWidth / 3.0f / _dig->getContentSize().width);
 38:     //设置锚点,中心点
 39:     _dig->setAnchorPoint(Vec2(.5f,.5f));
 40:     _flag->setAnchorPoint(Vec2(.5f,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 41:     _exitGame->setAnchorPoint(Vec2(.5f,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 42:     _restart->setAnchorPoint(Vec2(.5f,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 43:     //设置放置的位置
 44:
 45:     _dig->setPosition(Vec2(lowWidth * 0.25f,(lowHeight)* 0.75f));
 46:     _flag->setPosition(Vec2(lowWidth * 0.75f,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 47:     _exitGame->setPosition(Vec2(lowWidth * 0.75f,(lowHeight)* 0.25f));
 48:     _restart->setPosition(Vec2(lowWidth * 0.25f,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 49:     _dig->setEnabled(false);
 50:
 51: }
 52:
 53: //////////////////////////////////////////////////////////////////////////
 54: // Menu Call Back
 55: void GameScene::MenuCallDig(Ref*)
 56: {
 57:     _dig->setEnabled(false);
 58:     _flag->setEnabled(true);
 59:     _toFlag = false;
 60: }
 61:
 62: void GameScene::MenuCallFlag(Ref*)
 63: {
 64:     _dig->setEnabled(true);
 65:     _flag->setEnabled(false);
 66:     _toFlag = true;
 67: }
 68:
 69: void GameScene::MenuCallRestart(Ref*)
 70: {
 71:     Director::getInstance()->replaceScene(GameScene::scene(_customs));
 72: }
 73:
 74: void GameScene::MenuCallExit(Ref*)
 75: {
 76:     //Director::getInstance()->replaceScene(SelectCustoms::scene());
 77:     //CCDirector::sharedDirector()->end();
 78: }

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...