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

为了进一步了解背景音乐和音效播放的,咱们经过一个实例给你们介绍一下。以下图所示有两个场景:HelloWorld和Setting。在HelloWorld场景点击“游戏设置”菜单能够切换到Setting场景,在Setting场景中能够设置是否播放背景音乐和音效,设置完成后点击“OK”菜单能够返回到HelloWorld场景。 

 







咱们须要在AppDelegate中实现背景音乐播放暂停与继续函数,AppDelegate.h文件代码以下:
php

[html] view plaincopyhtml

 

  1. #ifndef  _APP_DELEGATE_H_  app

  2. #define  _APP_DELEGATE_H_  函数

  3.   

  4.   

  5. #include "cocos2d.h"  动画

  6. #include "SimpleAudioEngine.h"                                          ①  网站

  7.   

  8.   

  9. using namespace CocosDenshion;                                          ②  spa

  10. class  AppDelegate : private cocos2d::Application  .net

  11. {  orm

  12. public:  htm

  13.     AppDelegate();  

  14.     virtual ~AppDelegate();  

  15.   

  16.   

  17.     virtual bool applicationDidFinishLaunching();  

  18.     virtual void applicationDidEnterBackground();  

  19.     virtual void applicationWillEnterForeground();  

  20. };  

  21.   

  22.   

  23. #endif // _APP_DELEGATE_H_  



上述代码第①行是引入头文件SimpleAudioEngine.h,它是SimpleAudioEngine所须要的。第②行代码using namespace CocosDenshion是使用命名空间CocosDenshion,它是CocosDenshion引擎所须要的。

[html] view plaincopy

 

  1. #include "AppDelegate.h"  

  2. #include "HelloWorldScene.h"  

  3.   

  4.   

  5. USING_NS_CC;  

  6.   

  7.   

  8. AppDelegate::AppDelegate() {  

  9.   

  10.   

  11. }  

  12.   

  13.   

  14. AppDelegate::~AppDelegate()   

  15. {  

  16. }  

  17.   

  18.   

  19. bool AppDelegate::applicationDidFinishLaunching() {                             ①  

  20. … …  

  21.     // run  

  22.     director->runWithScene(scene);  

  23.       

  24.     //初始化 背景音乐  

  25.     SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3");      ②  

  26.     SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3");     ③  

  27.     //初始化 音效  

  28.     SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav");                   ④  

  29.   

  30.   

  31.     return true;  

  32. }  

  33.   

  34.   

  35. void AppDelegate::applicationDidEnterBackground() {                             ⑤  

  36.     Director::getInstance()->stopAnimation();  

  37.   

  38.   

  39.     SimpleAudioEngine::getInstance()->pauseBackgroundMusic();                    ⑥  

  40. }  

  41.   

  42.   

  43. void AppDelegate::applicationWillEnterForeground() {                                ⑦  

  44.     Director::getInstance()->startAnimation();  

  45.   

  46.   

  47.     SimpleAudioEngine::getInstance()->resumeBackgroundMusic();                   ⑧  

  48. }  



咱们在上述代码第①行是声明applicationDidFinishLaunching()函数,这个函数是在游戏启动时候调用。第②~④行代码是初始化背景音乐和音效文件。

第⑤行代码是声明applicationDidEnterBackground()是游戏进入到后天时候调用函数,在这个函数中须要中止动画和暂停背景音乐播放。第⑦行代码是声明applicationWillEnterForeground()是游戏从后天回到前台时候调用,在这个函数中须要继续动画和背景音乐播放。

 

 

更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》

本书交流讨论网站:http://www.cocoagame.net

欢迎加入cocos2d-x技术讨论群:25776038六、327403678

相关文章
相关标签/搜索