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

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

  1. #ifndef  _APP_DELEGATE_H_
  2. #define  _APP_DELEGATE_H_
  3. #include "cocos2d.h"
  4. #include "SimpleAudioEngine.h"                                          ①
  5. using namespace CocosDenshion;                                          ②
  6. class  AppDelegate : private cocos2d::Application
  7. {
  8. public:
  9. AppDelegate();
  10. virtual ~AppDelegate();
  11. virtual bool applicationDidFinishLaunching();
  12. virtual void applicationDidEnterBackground();
  13. virtual void applicationWillEnterForeground();
  14. };
  15. #endif // _APP_DELEGATE_H_

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

  1. #include "AppDelegate.h"
  2. #include "HelloWorldScene.h"
  3. USING_NS_CC;
  4. AppDelegate::AppDelegate() {
  5. }
  6. AppDelegate::~AppDelegate()
  7. {
  8. }
  9. bool AppDelegate::applicationDidFinishLaunching() {                             ①
  10. … …
  11. // run
  12. director->runWithScene(scene);
  13. //初始化 背景音乐
  14. SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3");      ②
  15. SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3");     ③
  16. //初始化 音效
  17. SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav");                   ④
  18. return true;
  19. }
  20. void AppDelegate::applicationDidEnterBackground() {                             ⑤
  21. Director::getInstance()->stopAnimation();
  22. SimpleAudioEngine::getInstance()->pauseBackgroundMusic();                    ⑥
  23. }
  24. void AppDelegate::applicationWillEnterForeground() {                                ⑦
  25. Director::getInstance()->startAnimation();
  26. SimpleAudioEngine::getInstance()->resumeBackgroundMusic();                   ⑧
  27. }

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

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

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

Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现的更多相关文章

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

    设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...

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

    HelloWorld场景就是游戏中的主菜单场景.HelloWorld.h文件代码如下: #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h& ...

  3. Cocos2d-x使用UserDefault数据持久化实例:保存背景音乐和音效设置

    UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活.UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵 ...

  4. Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)

    游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...

  5. android开发之背景音乐与音效

    android开发之背景音乐与音效 一:添加背景音乐(MediaPlayer) MediaPlayer class can be used to control playback of audio/v ...

  6. 将 SQL Server 实例设置为自动启动(SQL Server 配置管理器)

    本主题说明如何使用 SQL Server 配置管理器在 SQL Server 2012 中将 SQL Server 实例设置为自动启动. 在安装过程中,SQL Server 通常配置为自动启动. 如果 ...

  7. ORACLE默认实例设置--linux

    数据库实例多了之后,每次export的时候,显示的ORACLE_SID总不是自己经常用的那个,要是能让默认的自定义就好了. 现在就解释一下在linux环境中如何定义: 1.su - oracle 2. ...

  8. 关于Cocos2d-x中背景音乐和音效的添加

    1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...

  9. 一个filebeat实例 设置多topic设置

    方法1:一实例多topic: https://discuss.elastic.co/t/filebeat-5-0-output-to-kafka-multiple-topics/67934 The d ...

随机推荐

  1. 教你50招提升ASP.NET性能(二十一):避免使用会话状态

    (39)Avoid using session state 招数39: 避免使用会话状态 Where possible, you should try and avoid using session ...

  2. Android,iOS,浏览器打开手机QQ与指定用户聊天界面

    在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用.而在移动端腾讯貌似没有公布提供类似API,但是却可以使用schema模式来启动手机QQ. 以下为具体代码: 浏览器(包括手机浏览 ...

  3. URAL 1784 K - Rounders 找规律

    K - RoundersTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view. ...

  4. [Jest] Track project code coverage with Jest

    Jest comes pre-packaged with the ability to track code coverage for the modules you're testing, but ...

  5. 几种流行Webservice框架性能对照

     转自[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1      摘要 开发webservice应用程序中 ...

  6. Spark wordcount 编译错误 -- reduceByKey is not a member of RDD

    Attempting to run http://spark.apache.org/docs/latest/quick-start.html#a-standalone-app-in-scala fro ...

  7. ABAP屏幕基础

    Select语句的使用 关键字into后可以加 structure(结构体), internal table(内表) 和 fieldlist(字段列表) Authority 权限 程序员可以根据权限对 ...

  8. ROC和AUC介绍以及如何计算AUC

    原文:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ 为什么使用ROC曲线 既然已经这么多评价标准,为什么还要使用ROC和AUC呢?因 ...

  9. 关于try...catch...finally中return的疑惑

    原文:http://www.cnblogs.com/and_he/archive/2012/04/17/2453703.html 关于try...catch...finally里面的return一直是 ...

  10. 一元线性回归模型与最小二乘法及其C++实现

    原文:http://blog.csdn.net/qll125596718/article/details/8248249 监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等), ...