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

  1. #ifndef __Setting_SCENE_H__
  2. #define __Setting_SCENE_H__
  3. #include "cocos2d.h"
  4. #include "SimpleAudioEngine.h"
  5. class Setting : public cocos2d::Layer
  6. {
  7. bool isEffect;                                                      ①
  8. public:
  9. static cocos2d::Scene* createScene();
  10. virtual bool init();
  11. virtual void onEnter();
  12. virtual void onEnterTransitionDidFinish();
  13. virtual void onExit();
  14. virtual void onExitTransitionDidStart();
  15. virtual void cleanup();
  16. void menuSoundToggleCallback(cocos2d::Ref* pSender);
  17. void menuMusicToggleCallback(cocos2d::Ref* pSender);
  18. void menuOkCallback(cocos2d::Ref* pSender);
  19. CREATE_FUNC(Setting);
  20. };
  21. #endif // __Setting_SCENE_H__
  22. #define __HELLOWORLD_SCENE_H__

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

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

上述代码第①、③、⑧行是在判断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
 
05-11 03:31