【乐畅】工作积累 ---- 调节音量大小 (滑动条调节音量大小并保存起来 )

cocos2dx 的音量的大小的范围是 0.0f ~ 1.0f

 

 

 1     // 获取音量大小
 2     float musicVolume = getMusicVolume();
 3     float effectVolume = getEffectVolume();
 4 
 5     SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(musicVolume);
 6     SimpleAudioEngine::sharedEngine()->setEffectsVolume(effectVolume);
 7     
 8     // 音乐
 9     ControlSlider *sliderMusic = ControlSlider::create("Setting/ttttt.png", "Setting/ttttt.png", "Setting/ttdd.png");
10     sliderMusic->setPosition( Vec2(pBack->getContentSize().width * 0.6, pBack->getContentSize().height * 0.63));
11     // 设置滑动条的范围
12     sliderMusic->setMinimumValue(0.0f);
13     sliderMusic->setMaximumValue(100.0f);
14     // 设置当前滑动条的当前值
15     sliderMusic->setValue(musicVolume * 100.0f);
16     sliderMusic->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogSetting::changeMusic), Control::EventType::VALUE_CHANGED);
17     pBack->addChild(sliderMusic);
18 
19     // 音效
20     ControlSlider *sliderEffect = ControlSlider::create("Setting/ttttt.png", "Setting/ttttt.png", "Setting/ttdd.png");
21     sliderEffect->setPosition( Vec2(pBack->getContentSize().width * 0.6, pBack->getContentSize().height * 0.45));
22     // 设置滑动条的范围
23     sliderEffect->setMinimumValue(0.0f);
24     sliderEffect->setMaximumValue(100.0f);
25     // 设置当前滑动条的当前值
26     sliderEffect->setValue(effectVolume * 100.0f);
27     sliderEffect->addTargetWithActionForContro

回调事件

 1 void DialogSetting::changeMusic( Ref *pSender, Control::EventType event )
 2 {
 3     ControlSlider *target = static_cast<ControlSlider *>(pSender);
 4     float musicVolume = target->getValue() / 100.0f;
 5     SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(musicVolume);
 6     setMusicVolume(musicVolume);
 7 }
 8 
 9 void DialogSetting::changeEffect( Ref *pSender, Control::EventType event )
10 {
11     ControlSlider *target = static_cast<ControlSlider *>(pSender);
12     float effectVolume = target->getValue() / 100.0f;
13     SimpleAudioEngine::sharedEngine()->setEffectsVolume(effectVolume);
14     setEffectVolume(effectVolume);
15 }

存储数据

 1 float getMusicVolume()
 2 {
 3     char buff[32];
 4     sprintf(buff, "%s_MusicVolume", GameName);
 5     return GetFloatToXML(buff, 1.0f);
 6 }
 7 
 8 void setMusicVolume(float volume)
 9 {
10     char buff[32];
11     sprintf(buff, "%s_MusicVolume", GameName);
12     SetFloatToXML(buff, volume);
13     SaveFlush;
14 }
15 
16 float getEffectVolume()
17 {
18     char buff[32];
19     sprintf(buff, "%s_EffectVolume", GameName);
20     return GetFloatToXML(buff, 1.0f);
21 }
22 
23 void setEffectVolume(float volume)
24 {
25     char buff[32];
26     sprintf(buff, "%s_EffectVolume", GameName);
27     SetFloatToXML(buff, volume);
28     SaveFlush;
29 }

 

效果

 

 

 

posted @ 2016-07-19 14:52  silent-bobo  阅读(767)  评论(0编辑  收藏  举报