【囧希博客个人原创文章,欢迎讨论交友,如转载请注明出处:http://blog.csdn.net/yunpro】
【本段代码可以单独使用在一个场景中,作为一个独立完整的功能模块使用!】
可以在单独设定的页面中对音量,以及音效进行单独分别的设定。
【效果图】
【实例代码】
<span style="font-size:14px;">#include "ui/CocosGUI.h" USING_NS_CC; using namespace ui; Scene* AudioControl::createScene(){ auto scene =Scene::create(); auto layer =AudioControl::create(); scene->addChild(layer); return scene; } bool AudioControl::init(){ if(!Layer::init()){ return false; } //获得设备尺寸 Size visibleSize = Director::getInstance()->getVisibleSize(); //BGM条 //创建背景音乐文本,并添加为当前层的子节点 auto music_text = Text::create("音乐音量","Arial",32); music_text->setPosition(Vec2(visibleSize.width*0.25,visibleSize.height*0.7)); this->addChild(music_text); //创建一个滑动进度条 auto music_slider=Slider::create(); music_slider->loadBarTexture("sliderTrack.png"); music_slider->loadSlidBallTextures("sliderThumb.png","sliderThumb.png",""); music_slider->loadProgressBarTexture("sliderProgress.png"); //获取之前设置的音乐音量 float musicPercent = UserDefault::getInstance()->getFloatForKey("musicPercent"); // //第一次默认100 // if(musicPercent == 0.0f){ // musicPercent = 100.0f; // } //设置背景初始值 music_slider->setPercent(musicPercent); music_slider->cocos2d::Node::setPosition(Vec2(visibleSize.width*0.6,visibleSize.height*0.7)); //添加监听器 music_slider->addEventListener([=](Ref* pSender,Slider::EventType type){ //当滑块的百分比发生变化的时候 if(type== Slider::EventType::ON_PERCENTAGE_CHANGED){ //获得滑动条的百分比 int percent = music_slider->getPercent(); SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(float(percent)/100); UserDefault::getInstance()->setFloatForKey("musicPercent",percent); } }); this->addChild(music_slider); //EF条 //文 auto sound_text =Text::create("音效音量",32); sound_text->setPosition(Vec2(visibleSize.width*0.25,visibleSize.height*0.5)); this->addChild(sound_text); //条 auto effects_slider =Slider::create(); effects_slider->loadBarTexture("sliderTrack.png");//条材质 effects_slider->loadSlidBallTextures("sliderThumb.png",""); effects_slider->loadProgressBarTexture("sliderProgress.png"); float effectPercent = UserDefault::getInstance()->getFloatForKey("effectPercent");//获取之前设定EF值 // if (effectPercent == 0.0f) {//初次进入100 // effectPercent = 100.0f; // } effects_slider->setPercent(effectPercent);//设定初始值 effects_slider->cocos2d::Node::setPosition(Vec2(visibleSize.width*0.6,visibleSize.height*0.5)); effects_slider->addEventListener([=](Ref* pSender,Slider::EventType type){//获取,改变 if(type== Slider::EventType::ON_PERCENTAGE_CHANGED){ int percent = effects_slider->getPercent(); SimpleAudioEngine::getInstance()->setEffectsVolume(float(percent)/100); UserDefault::getInstance()->setFloatForKey("effectPercent",percent); } }); this->addChild(effects_slider); //返回 auto return_button = Button::create("b1.png"); return_button->setPosition(Vec2(visibleSize.width/2,visibleSize.width/2-300)); return_button->getContentSize().width/2; return_button->getContentSize().height/2; return_button->addTouchEventListener([](Ref* pSender,Widget::TouchEventType type){ if (type == Widget::TouchEventType::ENDED) { auto transition = TransitionSlideInL::create(0.3,GameMenu::createScene()); Director::getInstance()->replaceScene(transition); } }); this->addChild(return_button); //版权 auto banner_text = Text::create("『帮助』自动保存设置声量大小","Arial-BoldMT",32); banner_text->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.15)); this->addChild(banner_text); //网址 auto email_text = Text::create("wy66666666@qq.com",32); email_text->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.1)); this->addChild(email_text); return true; }</span>
本人热衷游戏程序制作,欢迎和我进行探讨与各种交流!
咱邮箱:wy66666666@qq.com
原文链接:https://www.f2er.com/cocos2dx/344236.html