关于cocos2dx的背景音乐和音效(2)

本文介绍了如何在Cocos2d-x 3.6版本中通过滑动条控制游戏的背景音乐和音效大小。详细讲解了创建标题、按钮和滑动条的代码,并实现点击按钮切换静音、滑动条改变音量的功能。还提到了在Cocos2d-x 3.6版本中存在无法调节音量的问题,并预告了解决方案。
摘要由CSDN通过智能技术生成

       上回我分享了音频的那些用法,这次来讲一些怎么用滑动条来控制音频。

       首先要新建个标题,以及自己找些控制的图片按钮,下面是初始化按钮的代码。

 

autolabhead=Label::createWithSystemFont("Option","华文楷体",60);                   //标题

   labhead->setTextColor(Color4B::WHITE);

   labhead->setPosition(Point(Director::getInstance()->getWinSize().width/2,

       Director::getInstance()->getWinSize().height/2+250));

   this->addChild(labhead);

 

   autolabmusic=Label::createWithSystemFont("Music","华文楷体",25);                   //music

   labmusic->setTextColor(Color4B::WHITE);

   labmusic->setPosition(Point(Director::getInstance()->getWinSize().width/2-280,

       Director::getInstance()->getWinSize().height/2+100));

   this->addChild(labmusic);

 

   autolabsound=Label::createWithSystemFont("Sound","华文楷体",25);                   //sound

   labsound->setTextColor(Color4B::WHITE);

   labsound->setPosition(Point(Director::getInstance()->getWinSize().width/2-280,

       Director::getInstance()->getWinSize().height/2-100));

   this->addChild(labsound);

 

   btnback=Button::create("option/back1.png","option/back2.png");                       //返回按钮

   btnback->setScale(0.1);

   btnback->setPosition(Point(Director::getInstance()->getWinSize().width-160,

       Director::getInstance()->getWinSize().height-100));

   this->addChild(btnback);

 

   btnmusic=Button::create("option/music1.png","option/music2.png");                   //音乐按钮

   btnmusic->setScale(0.1);

   btnmusic->setPosition(Point(Director::getInstance()->getWinSize().width/2+300,

       Director::getInstance()->getWinSize().height/2+100));

   this->addChild(btnmusic);

 

   btnsound=Button::create("option/sound1.png","option/sound2.png");                   //音效按钮

   btnsound->setScale(0.1);

   btnsound->setPosition(Point(Director::getInstance()->getWinSize().width/2+300,

       Director::getInstance()->getWinSize().height/2-100));

    this->addChild(btnsound);

 

        然后在新建两个滚动条,一个是音乐控制,一个是音效控制。

 

music_slider=Slider::create();

   music_slider->loadBarTexture("option/sliderTrack.png");

   music_slider->loadSlidBallTextures("option/sliderThumb.png","option/sliderThumb.png","");

   music_slider->loadProgressBarTexture("option/sliderProgress.png");

   //float musicpercent =UserDefault::getInstance()->getFloatForKey("musicPercent");

   floatmusicpercent1=0;

   if(musicpercent1==0.0f)

   {

       musicpercent1=100.0f;

   }

   music_slider->setPercent(musicpercent1);

   music_slider->setPosition(Point(Director::getInstance()->getWinSize().width/2,

       Director::getInstance()->getWinSize().height/2+100));

   this->addChild(music_slider);

 

 

   //音效

   musiceffect_slider=Slider::create();

   musiceffect_slider->loadBarTexture("option/sliderTrack.png");

   musiceffect_slider->loadSlidBallTextures("option/sliderThumb.png","option/sliderThumb.png","");

   musiceffect_slider->loadProgressBarTexture("option/sliderProgress.png");

   //float musicpercent = UserDefault::getInstance()->getFloatForKey("musicPercent");

   floatmusicpercent2=0;

   if(musicpercent2==0.0f)

   {

       musicpercent2=100.0f;

   }

   musiceffect_slider->setPercent(musicpercent2);

   musiceffect_slider->setPosition(Point(Director::getInstance()->getWinSize().width/2,

       Director::getInstance()->getWinSize().height/2-100));

    this->addChild(musiceffect_slider);

 

         新建所有按钮和滑动条之后,接下来就是实现功能了,先设置按钮的监听事件,点击以后静音,再次点击开启声音。

btnback->addClickEventListener([=](Ref*sender){                       //返回主界面

       SimpleAudioEngine::getInstance()->playEffect("music/buttoneffect.mp3");

       Director::getInstance()->popScene();

   });

 

   btnmusic->addClickEventListener([=](Ref*sender){                      //静音换图

       floatmusicv=SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();

       SimpleAudioEngine::getInstance()->playEffect("music/buttoneffect.mp3");

       if(musicv!=0)

       {

           SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0);

           music_slider->setPercent(0);

           btnmusic->loadTextureNormal("option/music3.png");

       }

       if(musicv==0)

       {

           SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(1);

           music_slider->setPercent(100);

           btnmusic->loadTextureNormal("option/music1.png");

       }

   });

 

   btnsound->addClickEventListener([=](Ref*sender){                      //静音效换图

       SimpleAudioEngine::getInstance()->playEffect("music/buttoneffect.mp3");

       floatsoundv=SimpleAudioEngine::getInstance()->getEffectsVolume();

       if(soundv!=0)

       {

           SimpleAudioEngine::getInstance()->setEffectsVolume(0);

           musiceffect_slider->setPercent(0);

           btnsound->loadTextureNormal("option/sound3.png");

       }

       if(soundv==0)

       {

           SimpleAudioEngine::getInstance()->setEffectsVolume(1);

           musiceffect_slider->setPercent(100);

           btnsound->loadTextureNormal("option/sound1.png");

       }

       

    });

 

        接下来设置滑动条同步设置声音的大小。

 

music_slider->addEventListener([=](Ref*pSender,Slider::EventTypetype)

   {

       if(type==Slider::EventType::ON_PERCENTAGE_CHANGED)

       {

           floatpece1=music_slider->getPercent();

           SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(pece1/100.0);

           //UserDefault::getInstance()->setFloatForKey("musicPercent",pece);

           if(pece1==0)

           {

               btnmusic->loadTextureNormal("option/music3.png");

           }

           else

           {

               btnmusic->loadTextureNormal("option/music1.png");

           }

       }

   });

 

   musiceffect_slider->addEventListener([=](Ref*pSender,Slider::EventTypetype)

   {

       if(type==Slider::EventType::ON_PERCENTAGE_CHANGED)

       {

           floatpece2=musiceffect_slider->getPercent();

           SimpleAudioEngine::getInstance()->setEffectsVolume(pece2/100.0);

           //UserDefault::getInstance()->setFloatForKey("musicPercent",pece);

           if(pece2==0)

           {

               btnsound->loadTextureNormal("option/sound3.png");

           }

           else

           {

               btnsound->loadTextureNormal("option/sound1.png");

           }

       }

    });

 

 

        这样就完成了我们控制声音的界面了。在这里我们用上一回的语句加入背景音乐和音效之后,就可已经在这个设置页面上面控制音效的大小和开关啦!(如果不会的话,请看我的这个博客:http://blog.csdn.net/shenmirenlcy/article/details/50054625)

        下面是运行的结果,请看图片。




        不过如果实在cocos2dx 3.6的版本中的话,可能你会发现貌似无法控制声音的大小,只能控制声音的开关,这个是因为在系统的函数里面,居然是空的........下一篇我再来分享一下怎么把里面的代码补全咯。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值