`
810364804
  • 浏览: 773472 次
文章分类
社区版块
存档分类
最新评论

【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用

 
阅读更多

本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010

UI在游戏中占有很重要的地位,但吸引玩家的除了这些看得到的界面和动画之外,游戏在后台中播放的背景音乐

游戏中打击、动作等音效的配合则会让游戏更受玩家的喜爱。在Cocos2D-X中也为我们封装了一个跨平台的、播放

音频相关的类SimpleAudioEngine,下面就让我们一起来学习这个类。

一、音乐与音效

由于Cocos2D-X是跨平台的引擎,所以如果大家想通过一套代码多平台通用播放音乐与音效,可以通过Cocos2D-X

定义的宏CC_TARGET_PLATFORM平台变量来区别播放的音频格式。比如,Android只支持ogg的音频格式。

SimpleAudioEngine的使用比较简单,此类不需要创建,只需要调用其中的播放、暂停、重复播放等与音乐、音效

相关的函数即可。

二、音乐与音效的相关函数

1、音乐相关的函数

<1> preloadBackgroundMusic:(NSString *) filePath

作用:预加载背景音乐文件。

参数:音乐文件的相对路经。

<2> playBackgroundMusic:(NSString *) filePath loop:(BOOL) loop

作用:播放背景音乐文件。

参数1:音乐文件的相对路经。

参数2:是否重复播放。

<3> stopBackgroundMusic()

作用:停止背景音乐。

<4> pauseBackgroundMusic()

作用:暂停背景音乐。

<5> resumeBackgroundMusic()

作用:继续播放背景音乐。

<6> rewindBackgroundMusic()

作用:倒带到音乐刚开始。

<7> isBackgroundMusicPlaying()

作用:获取当前背景音乐是否正在播放中。

<8> floatgetBackgroundMusicVolume()

作用:获取当前背景音乐的音量。

返回值:其音量范围[0.0,1.0]。

<9> setBackgroundMusicVolume(float volume)

作用:设置当前背景音乐的音量。

返回值:其音量范围[0.0,1.0]。

2、音效相关的函数

<1> preloadEffect(const char * pszFilePath)

作用:预设加载音效。

参数:音效文件的相对路经。

<2> unloadEffect(const char * pszFilePath)

作用:释放音效文件。

参数:音效文件的相对路经。

<3>unsigned intpreloadEffect(const char * pszFilePath,bool bLoop)

作用:播放音效文件。

参数1:音效文件的相对路经。

参数2:是否重复播放。

返回值:为播放的音效生成ID。

<4> stopEffect(unsigned int nSoundId)

作用:停止音效播放。

参数:需要停止音效的ID。

<5> pauseEffect(unsigned int nSoundId)

作用:暂停音效播放。

参数:需要暂停音效的ID。

<6> resumeEffect(unsigned int nSoundId)

作用:继续音效播放。

参数:需要继续播放音效的ID。

<7> float getEffectsVolume(unsigned int nSoundId)

作用:获取音效音量。

参数:音效音量范围[0.0,1.0]。

<8> setEffectsVolume(float volume)

作用:设置音效音量。

参数:音效音量范围[0.0,1.0]。

<9> pauseAllEffects()

作用:暂停所有音效。

<10> resumeAllEffects()

作用:继续播放所有音效。

<11> stopAllEffects()

作用:停止所有音效。

三、项目实例

1、首先新建Cocos2D-X项目,取名为“MyMusicEffect”,然后在HelloWorldScene.h文件中声明如下成员函数和变量。

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
   //menuitem回调函数
    void menuBack(CCObject * pSender);

    //用以记录音效ID
    int effectId ;

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};


2、然后在HelloWorldScene.cpp文件中添加如下所示代码。

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

//根据不同平台使用预编译索引不同音频文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define EFFECT_FILE        "effect2.ogg"
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
#define EFFECT_FILE        "effect1.raw"
#else
#define EFFECT_FILE        "effect1.wav"
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#define MUSIC_FILE        "music.mid"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
#define MUSIC_FILE        "background.ogg"
#else
#define MUSIC_FILE        "background.mp3"
#endif

std::string items[] = {
     "play background music",
        "stop background music",
        "pause background music",
        "resume background music",
        "rewind background music",
        "is background music playing",
        "play effect",
        "play effect repeatly",
        "stop effect",
        "unload effect",
        "add background music volume",
        "sub background music volume",
        "add effects volume",
        "sub effects volume",
        "pause effect",
        "resume effect",
        "pause all effects",
        "resume all effects",
        "stop all effects"
};

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {

        CC_BREAK_IF(! CCLayer::init());

       CCMenu* m_pItmeMenu = CCMenu::create();
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    int m_nTestCount = sizeof(items) / sizeof(items[0]);
    
    for (int i = 0; i < m_nTestCount; i++){
        CCLabelTTF* label = CCLabelTTF::create(items[i].c_str(), "Arial", 15);
        
        CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::menuBack));
        
        m_pItmeMenu->addChild(pMenuItem, i);
        pMenuItem->setPosition( CCPointMake(0, (s.height*0.5-20- (i + 1) * 15) ));
    }
    addChild(m_pItmeMenu,0,100);
    //预加载音乐和音效
    SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
    SimpleAudioEngine::sharedEngine()->preloadEffect(EFFECT_FILE);
    //设置默认音量
    SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
    SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuBack(CCObject * pSender){
    CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
    int nIdx = pMenuItem->getZOrder() ;
    switch(nIdx)
    {
        case 0:
            //播放背景音乐
            SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE , true);
            break;
        case 1:
            //停止背景音乐
            SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
            break;
        case 2:
            //暂停背景音乐
            SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
            break;
        case 3:
            //继续播放背景音乐
            SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
            break;
        case 4:
            //后退背景音乐
            SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
            break;
        case 5:
            //背景音乐是否正在播放
            if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()){
                CCLOG("背景音乐正在播放");
            }else{
                CCLOG("背景音乐没在播放");
            }
            break;
        case 6:
            //播放音效,并且得到此音效的ID
            effectId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
            break;
        case 7:
            //重复播放音效
            effectId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
            break;
        case 8:
            //停止音效播放
            SimpleAudioEngine::sharedEngine()->stopEffect(effectId);
            break;
        case 9:
            //释放音效
            SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
            break;
        case 10:
            //增加背景音乐的音量
            SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
            break;
        case 11:
            //减少背景音乐的音量
            SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
            break;
        case 12:
            //增加音效的音量
            SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
            break;
        case 13:
            //减少音效的音量
            SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
            break;
        case 14:
            //暂停音效
            SimpleAudioEngine::sharedEngine()->pauseEffect(effectId);
            break;
        case 15:
            //继续播放音效
            SimpleAudioEngine::sharedEngine()->resumeEffect(effectId);
            break;
        case 16:
            //暂停所有音效
            SimpleAudioEngine::sharedEngine()->pauseAllEffects();
            break;
        case 17:
            //继续所有音效
            SimpleAudioEngine::sharedEngine()->resumeAllEffects();
            break;
        case 18:
            //停止所有音效
            SimpleAudioEngine::sharedEngine()->stopAllEffects();
            break;
    }
}


3、实例效果图


源码下载地址

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics