Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现
为了进一步了解背景音乐和音效播放的,我们通过一个实例给大家介绍一下。如下图所示有两个场景:HelloWorld和Setting。在HelloWorld场景点击“游戏设置”菜单可以切换到Setting场景,在Setting场景中可以设置是否播放背景音乐和音效,设置完成后点击“OK”菜单可以返回到HelloWorld场景。
我们需要在AppDelegate中实现背景音乐播放暂停与继续函数,AppDelegate.h文件代码如下:
- #ifndef _APP_DELEGATE_H_
- #define _APP_DELEGATE_H_
- #include "cocos2d.h"
- #include "SimpleAudioEngine.h" ①
- using namespace CocosDenshion; ②
- class AppDelegate : private cocos2d::Application
- {
- public:
- AppDelegate();
- virtual ~AppDelegate();
- virtual bool applicationDidFinishLaunching();
- virtual void applicationDidEnterBackground();
- virtual void applicationWillEnterForeground();
- };
- #endif // _APP_DELEGATE_H_
上述代码第①行是引入头文件SimpleAudioEngine.h,它是SimpleAudioEngine所需要的。第②行代码using namespace CocosDenshion是使用命名空间CocosDenshion,它是CocosDenshion引擎所需要的。
- #include "AppDelegate.h"
- #include "HelloWorldScene.h"
- USING_NS_CC;
- AppDelegate::AppDelegate() {
- }
- AppDelegate::~AppDelegate()
- {
- }
- bool AppDelegate::applicationDidFinishLaunching() { ①
- … …
- // run
- director->runWithScene(scene);
- //初始化 背景音乐
- SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3"); ②
- SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3"); ③
- //初始化 音效
- SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav"); ④
- return true;
- }
- void AppDelegate::applicationDidEnterBackground() { ⑤
- Director::getInstance()->stopAnimation();
- SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); ⑥
- }
- void AppDelegate::applicationWillEnterForeground() { ⑦
- Director::getInstance()->startAnimation();
- SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); ⑧
- }
我们在上述代码第①行是声明applicationDidFinishLaunching()函数,这个函数是在游戏启动时候调用。第②~④行代码是初始化背景音乐和音效文件。
第⑤行代码是声明applicationDidEnterBackground()是游戏进入到后天时候调用函数,在这个函数中需要停止动画和暂停背景音乐播放。第⑦行代码是声明applicationWillEnterForeground()是游戏从后天回到前台时候调用,在这个函数中需要继续动画和背景音乐播放。
Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现的更多相关文章
- Cocos2d-x实例:设置背景音乐与音效-设置场景实现
设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...
- Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
HelloWorld场景就是游戏中的主菜单场景.HelloWorld.h文件代码如下: #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h& ...
- Cocos2d-x使用UserDefault数据持久化实例:保存背景音乐和音效设置
UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活.UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵 ...
- Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)
游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...
- android开发之背景音乐与音效
android开发之背景音乐与音效 一:添加背景音乐(MediaPlayer) MediaPlayer class can be used to control playback of audio/v ...
- 将 SQL Server 实例设置为自动启动(SQL Server 配置管理器)
本主题说明如何使用 SQL Server 配置管理器在 SQL Server 2012 中将 SQL Server 实例设置为自动启动. 在安装过程中,SQL Server 通常配置为自动启动. 如果 ...
- ORACLE默认实例设置--linux
数据库实例多了之后,每次export的时候,显示的ORACLE_SID总不是自己经常用的那个,要是能让默认的自定义就好了. 现在就解释一下在linux环境中如何定义: 1.su - oracle 2. ...
- 关于Cocos2d-x中背景音乐和音效的添加
1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...
- 一个filebeat实例 设置多topic设置
方法1:一实例多topic: https://discuss.elastic.co/t/filebeat-5-0-output-to-kafka-multiple-topics/67934 The d ...
随机推荐
- 教你50招提升ASP.NET性能(二十一):避免使用会话状态
(39)Avoid using session state 招数39: 避免使用会话状态 Where possible, you should try and avoid using session ...
- Android,iOS,浏览器打开手机QQ与指定用户聊天界面
在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用.而在移动端腾讯貌似没有公布提供类似API,但是却可以使用schema模式来启动手机QQ. 以下为具体代码: 浏览器(包括手机浏览 ...
- URAL 1784 K - Rounders 找规律
K - RoundersTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view. ...
- [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 ...
- 几种流行Webservice框架性能对照
转自[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1 摘要 开发webservice应用程序中 ...
- 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 ...
- ABAP屏幕基础
Select语句的使用 关键字into后可以加 structure(结构体), internal table(内表) 和 fieldlist(字段列表) Authority 权限 程序员可以根据权限对 ...
- ROC和AUC介绍以及如何计算AUC
原文:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ 为什么使用ROC曲线 既然已经这么多评价标准,为什么还要使用ROC和AUC呢?因 ...
- 关于try...catch...finally中return的疑惑
原文:http://www.cnblogs.com/and_he/archive/2012/04/17/2453703.html 关于try...catch...finally里面的return一直是 ...
- 一元线性回归模型与最小二乘法及其C++实现
原文:http://blog.csdn.net/qll125596718/article/details/8248249 监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等), ...