Chapter 6 - How to Play Music and Sound Effect
In this chapter, we would add background music to the game and play sound effect when the hero fires bullets.
Because there’s so few codes to add that we could say a little more about audio engine here. Cocos2d-x has wrapped SimpleAudioEngine to cross platforms. In our game, we are able to play music and sound effect using only one line of codes. It is so convenient. Of course, audio formats supported in different platform are different, about this issue, you could refer to http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Audio_formats_supported_by_CocosDenshion_on_different_platforms.
In fact, cocos2d-iphone includes the cocosDenshion library, and the library offers three layers of interfaces: CDSoundEngine->CDAudioManager->SimpleAudioEngine, but its realization totally relies on OpenAL. About OpenAL, it is not a standard of Khronos Group, it is an open source of Creative, which could be realize by software and hardware. So far, OpenAL is hardware realized only by Apple’s products, so in other platforms, we couldn’t provide supports for the lower layers of cocosDenshion, but we support the top layer which is used most commonly by the developers.
Now let’s get right to the issues.
First, copy the sound files background-music-aac.wav and pew-pew-lei.wav to the Resource directory. We use wav here because wav is supported in all platforms and these two files have been included in the Cocos2dSimpleGame project, you could download them from the bottom of this page.
Then include SimpleaudioEngine.h in HelloWorldScene.cpp.
1// cpp with cocos2d-x
2#include "SimpleAudioEngine.h"
Add the background music in init().
1// cpp with cocos2d-x
2CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(
3"background-music-aac.wav", true);
And play the sound effect in ccTouchesEnded() when the bullet is fired.
1// cpp with cocos2d-x
2CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(
3"pew-pew-lei.wav");
Ok, the audio addition is completed now.
Chapter 6 - How to Play Music and Sound Effect的更多相关文章
- Cocos2d-x之Sound
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 音效简介: 1.1 在游戏开发的过程中除了华丽的界面,生动的动画之外,适当的音效也是重要的一部分 1.2 游戏中的声音分为两类,一类是音乐 ...
- 脚本AI与脚本引擎
Scripted AI and Scripting Engines 脚本AI与脚本引擎 This chapter discusses some of the techniques you can us ...
- 用Phaser实现Flappy Bird 游戏
How to Make a Flappy Bird in HTML5 With Phaser - Part 1 Flappy Bird is a nice little game with easy ...
- Sprite Kit 入门教程
Sprite Kit 入门教程 Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...
- Approaching the Fun Factor in Game Design
I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...
- iOS开发——UI篇OC篇&SpriteKit详解
SpriteKit详解 SpriteKit,iOS/Mac游戏制作的新纪元 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或 ...
- Android UI 设计准则
Design Principles 设计准则 These design principles were developed by and for the Android User Experienc ...
- (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
转自:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 Upda ...
- Sprite Kit教程:制作一个通用程序 2
注1:本文译自Sprite Kit Tutorial: Making a Universal App: Part 2 目录 动画的定义:可行性 属性列表 添加游戏逻辑 添加音效 何去何从 上一篇文章中 ...
随机推荐
- String, StringBuffer, StringBuilder比较
1.见API: String是不可变的字符序列: StringBuffer是线程安全的,可变的字符序列: StringBuilder是可变的字符序列: StringBuffer与String的区别是S ...
- C++ Socket UDP "Hello World!"
这是C++ SOCKET网络程序中的C/S结构之UDP "Hello World !",共两个控制台工程: //////////////////////////////////// ...
- redis key和value数据类型
exists key del key1 key2 Redis 的vauleredis 提供五种数据类型:string,hash,list,set 及sorted set. string 是最基本的类型 ...
- nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)
http://acm.nyist.net/JudgeOnline/problem.php?pid=61 http://acm.nyist.net/JudgeOnline/problem.php?pid ...
- hadoop多机安装HA+YARN
HA 相比于Hadoop1.0,Hadoop 2.0中的HDFS增加了两个重大特性,HA(热备)和Federation(联邦).HA即为High Availability,用于解决NameNode单点 ...
- bzoj1559
自动机上状压dp,把单词是否存在压成二进制位注意这里面某些单词会包含其他单词,所以某些自动机上有些状态点对应多个二进制位方案只要再顺着有方案的状态搜一遍即可 ..,'a'..'z'] of longi ...
- Cocos2d-x 坑之二:目录改动后, cannot run on the selected destination
1:2dx开发中,目录改动后,经常会碰到这个提示错误: cannot run on the selected destination 解决方法:一般是因为 Info.plist文件属性问题,把 Ta ...
- android学习之BUG——The connection to adb is down, and a severe error has occured.
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- 英语之路 zt
各位为英语而郁闷的兄弟姐妹们: 自从考完GRE和TOEFL以后,心有所感,本想写点心得,但是因为太懒没写成.今日风雨如晦,心中又有所感,于是一舒笔墨,写下我学英语的方法.俺知道有很多兄弟姐妹们和曾经的 ...
- 初次运行 Git 前的配置
初次运行 Git 前的配置 一般在新的系统上,我们都需要先配置下自己的 Git 工作环境.配置工作只需一次,以后升级时还会沿用现在的配置.当然,如果需要,你随时可以用相同的命令修改已有的配置. Git ...