
扫雷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*)
@H_187_404@ 70: { 71: Director::getInstance()->replaceScene(GameScene::scene(_customs)); 72: } @H_5_419@ 73:
74: voID GameScene::MenuCallExit(Ref*)
75: { 76: //Director::getInstance()->replaceScene(SelectCustoms::scene());
77: //CCDirector::sharedDirector()->end();
78: } 总结 以上是内存溢出为你收集整理的cocos3.x 扫雷04 菜单项的添加全部内容,希望文章能够帮你解决cocos3.x 扫雷04 菜单项的添加所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)