zoukankan      html  css  js  c++  java
  • Chapter 6

     

    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.

  • 相关阅读:
    北斗授时系统,GPS授时服务器—在行业应用及介绍
    GPS时间同步服务器,NTP时间同步服务器,——数据采集系统应用
    北斗卫星时钟(北斗授时服务器)厂家介绍及价格分析
    linux系统下ntp网络时钟服务器(NTP服务器)的搭建和使用
    2020年SAP项目艰辛曲折的开工历程 III
    2020年SAP项目艰辛曲折的开工历程 II
    2020年SAP项目艰辛曲折的开工历程 I
    2020年肺炎疫情期间看的几部古装电视剧
    做人不忘本,才能走得更远 --- 我看电视剧《雍正王朝》
    邬先生及时功成身退,是明哲保身的聪明做法 --- 我看电视剧《雍正王朝》
  • 原文地址:https://www.cnblogs.com/yssgyw/p/3224267.html
Copyright © 2011-2022 走看看