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的更多相关文章

  1. Cocos2d-x之Sound

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 音效简介: 1.1 在游戏开发的过程中除了华丽的界面,生动的动画之外,适当的音效也是重要的一部分 1.2 游戏中的声音分为两类,一类是音乐 ...

  2. 脚本AI与脚本引擎

    Scripted AI and Scripting Engines 脚本AI与脚本引擎 This chapter discusses some of the techniques you can us ...

  3. 用Phaser实现Flappy Bird 游戏

    How to Make a Flappy Bird in HTML5 With Phaser - Part 1 Flappy Bird is a nice little game with easy ...

  4. Sprite Kit 入门教程

    Sprite Kit 入门教程  Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...

  5. Approaching the Fun Factor in Game Design

    I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...

  6. iOS开发——UI篇OC篇&SpriteKit详解

    SpriteKit详解 SpriteKit,iOS/Mac游戏制作的新纪元 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或 ...

  7. Android UI 设计准则

    Design Principles  设计准则 These design principles were developed by and for the Android User Experienc ...

  8. (转)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 ...

  9. Sprite Kit教程:制作一个通用程序 2

    注1:本文译自Sprite Kit Tutorial: Making a Universal App: Part 2 目录 动画的定义:可行性 属性列表 添加游戏逻辑 添加音效 何去何从 上一篇文章中 ...

随机推荐

  1. [topcoder]KingdomReorganization

    http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...

  2. 2016 Web 开发资源工具大搜罗

    原文链接: 100+ Awesome Web Development Tools and Resources Web的迅猛发展对于开发者来说既是一件好事也是一件坏事.不断紧跟潮流学习新的技术.编程语言 ...

  3. 程序异常捕获库 - CrashRpt

    CrashRpt.dll用来在应用程序出现异常crash时,捕获到错误. 并收集出错信息: MiniDump文件.硬件信息.系统信息.出错信息.进程信息.服务信息.驱动信息.启动信息.软件列表.端口信 ...

  4. 转载:win7JDK环境配置

    [win7JDK环境配置] 网址:http://blog.sina.com.cn/s/blog_6a9df2330100ms9q.html 系统变量下: (1) 新建->变量名:JAVA_HOM ...

  5. Linux 下报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must解决方案

    一.报错环境:在Linux mint下,前几天还用得很好的的eclipse,今天开机不知为什么这样. Linux 下报错:A Java RunTime Environment (JRE) or Jav ...

  6. WordPress RokIntroScroller插件‘thumb.php’多个安全漏洞

    漏洞名称: WordPress RokIntroScroller插件‘thumb.php’多个安全漏洞 CNNVD编号: CNNVD-201309-383 发布时间: 2013-09-24 更新时间: ...

  7. 冒烟测试、α测试、Beta测试、性能测试

    “冒烟测试”(也可称为showcase)这一术语描述的是在将代码更改嵌入到产品的源树中之前对这些更改进行验证的过程. 冒烟测试(smoke test)在测试中发现问题,找到了一个Bug,然后开发人员会 ...

  8. Parallels Desktop 7用bootcamp安装win7 后如何激活WIN7

    在parallels的选项中,在硬件选项卡的引导标记中添加:kernel.waet.enable=0 ,重新启动win7即可激活.

  9. 开发服务器端——工程配置

    概述:    服务器开发,一般分为 主程序类Project(主函数main) 流程控制类Kernel(处理收到的数据) 网络类Network(数据的收发) 公共类Common(定义一些常量和结构体,自 ...

  10. Mac下快捷键列表

    主界面 command + tab 切换程序 command + ` 在程序内切换界面 command + w 关闭界面 command + q 关闭程序 command + option + esc ...