首页 > 代码库 > Cocos2d-x实例:设置背景音乐与音效-设置场景实现

Cocos2d-x实例:设置背景音乐与音效-设置场景实现

设置场景(Setting),Setting.h文件代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #ifndef __Setting_SCENE_H__  
  2. #define __Setting_SCENE_H__  
  3.   
  4.   
  5. #include "cocos2d.h"  
  6. #include "SimpleAudioEngine.h"  
  7.   
  8.   
  9. class Setting : public cocos2d::Layer  
  10. {  
  11.     bool isEffect;                                                      ①  
  12. public:  
  13.     static cocos2d::Scene* createScene();  
  14.     virtual bool init();    
  15.     virtual void onEnter();  
  16.     virtual void onEnterTransitionDidFinish();  
  17.     virtual void onExit();  
  18.     virtual void onExitTransitionDidStart();  
  19.     virtual void cleanup();  
  20.       
  21.     void menuSoundToggleCallback(cocos2d::Ref* pSender);  
  22.     void menuMusicToggleCallback(cocos2d::Ref* pSender);  
  23.     void menuOkCallback(cocos2d::Ref* pSender);  
  24.       
  25.     CREATE_FUNC(Setting);  
  26. };  
  27.   
  28.   
  29. #endif // __Setting_SCENE_H__  
  30.   
  31.   
  32. #define __HELLOWORLD_SCENE_H__  





上述代码第①是定义布尔类型成员变量isEffect,它用来保存现在是否允许音效,事实上是否允许音效和背景音乐播放状态应该持久化到文件中,由于我们还没有介绍数据持久化,所以我们采用isEffect变量记录状态,当我们在后面的章节学习完成数据持久化后,再保存它们状态。其它的代码与HelloWorld场景代码非常相似,这里不再赘述。Setting.cpp文件代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #include "SettingScene.h"  
  2.   
  3.   
  4. USING_NS_CC;  
  5. using namespace CocosDenshion;  
  6.   
  7.   
  8. … …  
  9. bool Setting::init()  
  10. {  
  11.     if ( !Layer::init() )  
  12.     {  
  13.         return false;  
  14.     }  
  15.   
  16.   
  17.     log("Setting init");  
  18.       
  19.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  20.     Point origin = Director::getInstance()->getVisibleOrigin();  
  21.   
  22.   
  23.     Sprite *bg = Sprite::create("setting-back.png");  
  24.       
  25.     bg->setPosition(Point(origin.x + visibleSize.width/2,  
  26.                              origin.y + visibleSize.height /2));  
  27.     this->addChild(bg);  
  28.   
  29.   
  30.     //音效  
  31.     auto soundOnMenuItem = MenuItemImage::create(  
  32.                              "on.png",  
  33.                              "on.png");  
  34.     auto soundOffMenuItem = MenuItemImage::create(  
  35.                              "off.png",  
  36.                              "off.png");  
  37.       
  38.     auto soundToggleMenuItem = MenuItemToggle::createWithCallback(  
  39.                                 CC_CALLBACK_1(Setting::menuSoundToggleCallback, this),   
  40.                                                 soundOffMenuItem,  
  41.                                                 soundOnMenuItem,   
  42.                                                 NULL);  
  43.     soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 220)));  
  44.       
  45.     //背景音乐  
  46.     auto musicOnMenuItem  = MenuItemImage::create(  
  47.                              "on.png",  
  48.                              "on.png");  
  49.     auto musicOffMenuItem  = MenuItemImage::create(  
  50.                              "off.png",  
  51.                              "off.png");  
  52.     auto musicToggleMenuItem = MenuItemToggle::createWithCallback(  
  53.                                 CC_CALLBACK_1(Setting::menuMusicToggleCallback, this),  
  54.                                                     musicOffMenuItem,  
  55.                                                     musicOnMenuItem,  
  56.                                                     NULL );  
  57.     musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 362)));  
  58.   
  59.   
  60.     //Ok按钮  
  61.     auto okMenuItem  = MenuItemImage::create(   
  62.                              "ok-down.png",  
  63.                              "ok-up.png",  
  64.                              CC_CALLBACK_1(Setting::menuOkCallback, this));  
  65.   
  66.   
  67.     okMenuItem->setPosition(Director::getInstance()->convertToGL(Point(600, 510)));  
  68.   
  69.   
  70.     Menu* mn = Menu::create(soundToggleMenuItem, musicToggleMenuItem,okMenuItem, NULL);  
  71.     mn->setPosition(Point::ZERO);  
  72.     this->addChild(mn);  
  73.   
  74.   
  75.     return true;  
  76. }  
  77.   
  78.   
  79. void Setting::menuOkCallback(Ref* pSender)  
  80. {  
  81.     Director::getInstance()->popScene();  
  82.     if (isEffect) {  
  83.         SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ①  
  84.     }  
  85. }  
  86.   
  87.   
  88. void Setting::menuSoundToggleCallback(Ref* pSender)                         ②  
  89. {  
  90.     auto soundToggleMenuItem = (MenuItemToggle*)pSender;  
  91.     log("soundToggleMenuItem %d", soundToggleMenuItem->getSelectedIndex());  
  92.   
  93.   
  94.     if (isEffect) {  
  95.         SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ③  
  96.     }  
  97.   
  98.   
  99.     if (soundToggleMenuItem->getSelectedIndex() == 1) {//选中状态Off -> On                ④  
  100.         isEffect = false;  
  101.     } else {  
  102.         isEffect = true;  
  103.         SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ⑤  
  104.     }  
  105. }  
  106.   
  107.   
  108. void Setting::menuMusicToggleCallback(Ref* pSender)                         ⑥  
  109. {  
  110.     auto musicToggleMenuItem = (MenuItemToggle*)pSender;  
  111.     log("musicToggleMenuItem %d", musicToggleMenuItem->getSelectedIndex());  
  112.       
  113.     if (musicToggleMenuItem->getSelectedIndex() == 1) {//选中状态Off -> On                ⑦  
  114.         SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Synth.mp3");  
  115.     } else {  
  116.         SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3");  
  117.     }  
  118.     if (isEffect) {  
  119.         SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ⑧  
  120.     }  
  121. }  
  122.   
  123.   
  124. void Setting::onEnter()  
  125. {  
  126.     Layer::onEnter();  
  127.     log("Setting onEnter");  
  128. }  
  129.   
  130.   
  131. void Setting::onEnterTransitionDidFinish()  
  132. {  
  133.     Layer::onEnterTransitionDidFinish();  
  134.     log("Setting onEnterTransitionDidFinish");    
  135.     isEffect = true;  
  136.     //播放  
  137.     SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3", true);      ⑨  
  138. }  
  139.   
  140.   
  141. void Setting::onExit()  
  142. {  
  143.     Layer::onExit();  
  144.     log("Setting onExit");    
  145. }  
  146.   
  147.   
  148. void Setting::onExitTransitionDidStart()  
  149. {  
  150.     Layer::onExitTransitionDidStart();  
  151.     log("Setting onExitTransitionDidStart");  
  152. }  
  153.   
  154.   
  155. void Setting::cleanup()  
  156. {  
  157.     Layer::cleanup();  
  158.     log("Setting cleanup");   
  159.     //停止  
  160.     SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Synth.mp3");            ⑩  
  161. }  



上述代码第①、③、⑧行是在判断isEffect为true(音效播放开关打开)情况下播放音效。
代码第②行menuSoundToggleCallback是用户点击音效开发按钮时候回调函数,其中第④行代码是判断是否按钮状态从Off -> On,如果是则将开关变量isEffect 设置为false,否则为true,而且通过第⑤行代码SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav")播放一次音效。
代码第⑥行menuMusicToggleCallback是用户点击背景音乐按钮时候回调函数,其中第⑦行代码是判断是否按钮状态从Off -> On,如果是则停止播放背景音乐,否则为开始播放。

第⑧行代码是onEnterTransitionDidFinish()函数中播放背景音乐。第⑨行代码是cleanup()函数中停止播放背景音乐。

 

 

 

更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678