论坛首页 移动开发技术论坛

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

浏览 937 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-08-05  

设置场景(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. border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !important; padding
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics