1、配置页面:
可以看到,配置页面是有两个滑动条,和两个开关组成。
cocos2d-x在扩展类中提供了滑动条的这么一个组件:CCControlSlider,其主要的设置函数如下
1.setValue(float value); //设置当前滑动条值
2.getValue(); //获取当前滑动条值
3.setMinimumValue(float value); //设置滑动条最小滑动值
4.setMaximumAllowedValue(float value); //设置滑动条允许的最小值
5.setMaximumValue(float value); //设置滑动条最大值
6.setMaximumAllowedValue(float value); //设置滑动条允许的最大值
主实现如下
std::string music = STATIC_DATA_STRING("music");
Config::sharedConfig()->GBKToUTF8(music);
CCLabelBMFont* musicLabel = CCLabelBMFont::create(music.c_str(), hw_font_fnt);
musicLabel->setOpacity(200);
musicLabel->setAnchorPoint(CCPointZero);
musicLabel->setPosition(ccp(ScreenWidth * 0.35, ScreenHeight * 0.6 - 10));
this->addChild(musicLabel);
//初始化滑动条(三个参数分别对应底部,进度条,控制器三个精灵)
CCControlSlider* musicSlider = CCControlSlider::create(CCSprite::createWithSpriteFrameName(slider_bar_png),
CCSprite::createWithSpriteFrameName(silder_progressBar_png), CCSprite::createWithSpriteFrameName(slider_bar_button_png));
musicSlider->setScaleX(0.5f);
musicSlider->setMaximumValue(1);
musicSlider->setMinimumValue(0);
musicSlider->setMaximumAllowedValue(1);
musicSlider->setMinimumAllowedValue(0);
//添加响应事件
musicSlider->addTargetWithActionForControlEvents(this, cccontrol_selector(SettingsLayer::onVolumnChange), CCControlEventValueChanged);
musicSlider->setAnchorPoint(CCPointZero);
musicSlider->setPosition(ccp(ScreenWidth * 0.5, ScreenHeight * 0.6));
musicSlider->setValue(Config::sharedConfig()->getMusicVolume());
musicSlider->setTag(1);
this->addChild(musicSlider);
由于cocos2d-x的音量的控制范围是0到1,所以滑动条的最小和最大值要设置为0和1
而对于开关菜单项则可以使用CCMenuItemToggle
std::string yes = STATIC_DATA_STRING("yes");
Config::sharedConfig()->GBKToUTF8(yes);
std::string no = STATIC_DATA_STRING("no");
Config::sharedConfig()->GBKToUTF8(no);
CCMenuItemFont* yesFontItem1 = CCMenuItemFont::create(yes.c_str());
yesFontItem1->setOpacity(200);
yesFontItem1->setScale(0.8f);
CCMenuItemFont* noFontItem1 = CCMenuItemFont::create(no.c_str());
noFontItem1->setOpacity(200);
noFontItem1->setScale(0.8f);
std::string tsuihikidanMode = STATIC_DATA_STRING("tsuihikidan_mode");
Config::sharedConfig()->GBKToUTF8(tsuihikidanMode);
CCLabelBMFont* tsuihikidanModeLabel = CCLabelBMFont::create(tsuihikidanMode.c_str(), hw_font_fnt);
tsuihikidanModeLabel->setOpacity(200);
tsuihikidanModeLabel->setAnchorPoint(CCPointZero);
tsuihikidanModeLabel->setPosition(ccp(ScreenWidth * 0.35, ScreenHeight * 0.4 - 10));
this->addChild(tsuihikidanModeLabel);
CCMenuItemToggle* tsuihikidanModeItem = CCMenuItemToggle::createWithTarget(this,
menu_selector(SettingsLayer::tsuihikidanMode),
yesFontItem1,
noFontItem1,
NULL );
tsuihikidanModeItem->setAnchorPoint(CCPointZero);
if(Config::sharedConfig()->getIsTsuihikidanMode()) {
tsuihikidanModeItem->setSelectedIndex(0);
}else {
tsuihikidanModeItem->setSelectedIndex(1);
}
CCMenu* tsuihikidanModeMenu = CCMenu::create(tsuihikidanModeItem, NULL);
tsuihikidanModeMenu->setAnchorPoint(CCPointZero);
tsuihikidanModeMenu->setPosition(ccp(ScreenWidth * 0.55, ScreenHeight * 0.4 - 10));
this->addChild(tsuihikidanModeMenu);
触发监听事件时可以通过getSelectedIndex方法来获取开关是否开启
void SettingsLayer::tsuihikidanMode(CCObject* pSender) {
if(((CCMenuItemToggle*)pSender)->getSelectedIndex() == 0) {
Config::sharedConfig()->setIsTsuihikidanMode(true);
}else {
Config::sharedConfig()->setIsTsuihikidanMode(false);
}
}