#include "MenuScene.h"
USING_NS_CC;
Scene* MenuScene::createScene()
{
auto scene = Scene::create();
auto layer = MenuScene::create();
scene->addChild(layer);
return scene;
}
bool MenuScene::init()
{
if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255)) )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
addBackgroundSprite();
addMenuSprites();
return true;
}
void MenuScene::addBackgroundSprite()
{
Sprite* menuSpriteLogo= Sprite::create(MENU_LOGO);
menuSpriteLogo->setPosition(ccp(visibleSize.width/2,visibleSize.height));
menuSpriteLogo->setAnchorPoint(ccp(0.5,1));
menuSpriteLogo->setScale(0.6f);
addChild(menuSpriteLogo);
Sprite* rainBowSprite= Sprite::create(RAINBOW);
rainBowSprite->setPosition(ccp(5,visibleSize.height-20));
rainBowSprite->setAnchorPoint(ccp(0,1));
rainBowSprite->setScale(0.3f);
addChild(rainBowSprite);
MoveBy* rainBowMove = MoveBy::create(1,ccp(8,0));
MoveBy* rainBowMoveReverse = rainBowMove->reverse();
Sequence* rainBowAction = Sequence::create(rainBowMove,rainBowMoveReverse,NULL);
rainBowSprite->runAction(RepeatForever::create(rainBowAction));
}
void MenuScene:: addMenuSprites()
{
Scale9Sprite* btnNormal = Scale9Sprite::create(NORMAL_MENU);
Scale9Sprite* btnPress = Scale9Sprite::create(PRESS_MENU);
LabelTTF* singleGameTTF = LabelTTF::create(SINGLE_GAME ,FONT_MENU,Btn_FontSize);
ControlButton* singleGameBtn = ControlButton::create(singleGameTTF,btnNormal);
singleGameBtn->setBackgroundSpriteForState(btnPress, Control::State::SELECTED);
singleGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-200));
singleGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));
singleGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);
singleGameBtn->setTag(Btn_Single_Game_TAG);
addChild(singleGameBtn);
Scale9Sprite* btnNormal2 = Scale9Sprite::create(NORMAL_MENU);
Scale9Sprite* btnPress2 = Scale9Sprite::create(PRESS_MENU);
LabelTTF* multiGameTTF = LabelTTF::create(MULTI_GAME,FONT_MENU,Btn_FontSize);
ControlButton* multiGameBtn = ControlButton::create(multiGameTTF,btnNormal2);
multiGameBtn->setBackgroundSpriteForState(btnPress2, Control::State::HIGH_LIGHTED);
multiGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-260));
multiGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));
multiGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);
multiGameBtn->setTag(Btn_Multi_Game_TAG);
addChild(multiGameBtn);
Scale9Sprite* btnNormal3 = Scale9Sprite::create(NORMAL_MENU);
Scale9Sprite* btnPress3 = Scale9Sprite::create(PRESS_MENU);
bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);
LabelTTF* settingsGameTTF;
if(music_on)
{
settingsGameTTF = LabelTTF::create(MUSIC_ON,FONT_MENU,Btn_FontSize);
}else
{
settingsGameTTF = LabelTTF::create(MUSIC_OFF,FONT_MENU,Btn_FontSize);
}
ControlButton* settingsGameBtn = ControlButton::create(settingsGameTTF,btnNormal3);
settingsGameBtn->setBackgroundSpriteForState(btnPress3, Control::State::SELECTED);
settingsGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-320));
settingsGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));
settingsGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);
settingsGameBtn->setTag(Btn_Music_TAG);
addChild(settingsGameBtn);
Scale9Sprite* btnNormal4 = Scale9Sprite::create(NORMAL_MENU);
Scale9Sprite* btnPress4 = Scale9Sprite::create(PRESS_MENU);
LabelTTF* quitGameTTF = LabelTTF::create(QUIT_GAME,FONT_MENU,Btn_FontSize);
ControlButton* quitGameBtn = ControlButton::create(quitGameTTF,btnNormal4);
quitGameBtn->setBackgroundSpriteForState(btnPress4, Control::State::SELECTED);
quitGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-380));
quitGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));
quitGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);
quitGameBtn->setTag(Btn_Quit_Game_TAG);
addChild(quitGameBtn);
}
void MenuScene:: menuTouchDown(Object* pSender,Control::EventType event)
{
log("single touched");
ControlButton* button = (ControlButton*)pSender;
int tag = button->getTag();
/*
if(tag == Btn_Single_Game_TAG)
{
log("single game");
return;
}
if(tag == Btn_Multi_Game_TAG)
{
log("multi game");
return;
}
if(tag == Btn_Music_TAG)
{
bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);
if(music_on)
{
UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,false);
button->setTitleForState(MUSIC_OFF,Control::State::NORMAL);
}else
{
UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,true);
button->setTitleForState(MUSIC_ON,Control::State::NORMAL);
}
return;
}
if(tag == Btn_Quit_Game_TAG)
{
log("quit game");
return;
}*/
switch(tag)
{
case Btn_Single_Game_TAG:
{
log("single game");
Director::getInstance()->pushScene(MapChooseScene::createScene());
}
case Btn_Multi_Game_TAG:
{
log("multi game");
break;
}
case Btn_Music_TAG:
{
bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);
if(music_on)
{
UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,false);
button->setTitleForState(MUSIC_OFF,Control::State::NORMAL);
}else
{
UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,true);
button->setTitleForState(MUSIC_ON,Control::State::NORMAL);
}
break;
}
case Btn_Quit_Game_TAG:
{
log("quit game");
popupLayer();
break;
}
default:
break;
}
}
void MenuScene::popupLayer(){
// 定义一个弹出层,传入一张背景图
PopupLayer* popDialog = PopupLayer::create(DIALOG_BG);
// ContentSize 是可选的设置,可以不设置,如果设置把它当作 9 图缩放
popDialog->setContentSize(CCSizeMake(Quit_Dialog_Size_Width, Quit_Dialog_Size_Height));
popDialog->setTitle(DIALOG_TITLE);
popDialog->setContentText(DIALOG_CONTENT, 20, 60, 250);
// 设置回调函数,回调传回一个 CCNode 以获取 tag 判断点击的按钮
// 这只是作为一种封装实现,如果使用 delegate 那就能够更灵活的控制参数了
popDialog->setCallbackFunc(this, callfuncN_selector(MenuScene::quitButtonCallback));
// 添加按钮,设置图片,文字,tag 信息
popDialog->addButton(BUTTON_BG1, BUTTON_BG3, OK, Btn_Quit_OK_TAG);
popDialog->addButton(BUTTON_BG2, BUTTON_BG3, CANCEL, Btn_Quit_Cancel_TAG);
// 添加到当前层
this->addChild(popDialog);
}
void MenuScene::quitButtonCallback(Node *pNode)
{
if(pNode->getTag() == Btn_Quit_OK_TAG)
{
Director::getInstance()->end();
}else
{
return;
}
}
Cocos2d-x 3.2 大富翁游戏项目开发ScrollView关卡选择
5星 · 超过95%的资源 需积分: 9 141 浏览量
2014-12-17
00:32:02
上传
评论
收藏 1.36MB RAR 举报
lideguo1979
- 粉丝: 148
- 资源: 77
最新资源
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
- 足球比赛结果统计表2006-2011年大约28W场比赛
- 基于PHP+mysql的社区交流系统(源代码)
- yolov5,SSD 可能使用到的一些代码
- 一键批量生成多层次文件夹结构,使用Python脚本实现嵌套文件夹批量生成
- 基于c51单片机+DS1302+DHT11温湿度模块+LCD1602显示的万年历硬件原理图+BOM+软件程源码序+仿真图.zip
- NSGA2的MATLAB代码
- Messagepassingtest_GCN_DGL.py
- Sh,Docker 运维好帮手,一招通过 sh 脚本批量快速启动和重启多个Docker 容器
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈