您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x播放背景音乐和战斗音效

2014-02-14 10:44 393 查看
cocos2d-x播放背景音乐和战斗音效

在游戏中,音乐是一个不可缺少的部分,音乐分为游戏中的长时间的背景音乐和短的音效两种,可以为我们的游戏增加效果。Cocos2d-x支持多种音乐格式mp3,wav等音乐格式

1.背景音乐

要使用一个音乐,首先要预加载这个音乐,预加载的方法如下

SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::fullPathFromRelativePath(MUSIC_FILE) );

MUSIC_FILE就是音乐文件相对Res文件夹的相对路径

然后就可以播放了,播放的方法如下

SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);

第一个参数中的MUSIC_FILE就是音乐文件相对Res文件夹的相对路径,然后再转换为绝对路径,第二个参数就是是否循环播放,由于是背景音乐,我们定为true

其他函数

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();//停止背景音乐,可以代一个布尔型参数,表示是否释放音乐文件

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();//暂停背景音乐

SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();//重头调用背景音乐

SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()//返回布尔型参数,是否在放着背景音乐

SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);//设置音量0.0-1.0

2.音效

使用音效,和使用音乐类似,也需要预加载,用法如下

SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::fullPathFromRelativePath(EFFECT_FILE) );

EFFECT_FILE就是音乐的相对Res文件夹的相对路径

然后播放就可以了

m_nSoundId=SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());

m_nSoundId=SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);

和背景音乐一样,第二个参数是可选的,如果是true就是循环播放,否则就是只播一次

其他函数

SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);//停止音效,可以选择单独停掉一个音效,通过创建时的m_nSoundId停止

SimpleAudioEngine::sharedEngine()->stopAllEffects();//停止全部音效

SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);//暂停单个音效

SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);//重新开始音效

SimpleAudioEngine::sharedEngine()->pauseAllEffects();//暂停全部音效

SimpleAudioEngine::sharedEngine()->resumeAllEffects();//重新开始全部音效

SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);//设置音效音量

SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());//卸载音效

最后要记住,在程序不需要音乐时,要SimpleAudioEngine::sharedEngine()->end();释放sharedEngine()

我写了一个播放音乐的类,在这里和大家分享一下

[cpp] view
plaincopy

<span style="font-size:18px;">//

// SGPlayMusic.h

// GameSanGuo

//

// Created by 江南岸 on 13-5-16.

//

//

#define MUSIC_PLAY(__xxx__) SGPlayMusic::sharePlayMusic()->playerMusicByType(__xxx__);

#define MUSIC_STOP SGPlayMusic::sharePlayMusic()->stopMusic();

#define MUSIC_VOLUME(__xxxx__) SGPlayMusic::sharePlayMusic()->setVolume(__xxxx__)

#define EFFECT_PLAY(__xxxx__,__yyyy__) SGPlayMusic::sharePlayMusic()->playEffectByType(__xxxx__, __yyyy__);

#define EFFECT_STOP SGPlayMusic::sharePlayMusic()->stopEffect();

#define EFFCT_VOLUME(__xxxx__) SGPlayMusic::sharePlayMusic()->setEffectVolume(__xxxx__);

#define MUSIC_PVP "music/pvp.mp3" //pvp音乐

#define MUSIC_NORMAL "music/narmal.mp3" //正常战斗音乐

#define MUSIC_BOSS "music/boss.mp3" //boss战音乐

#define MUSIC_SCENE "music/scene.mp3" //场景音乐

#define MUSIC_TEST "music/祁隆、庄心妍 - 一万个舍不得.mp3"

#ifndef __GameSanGuo__SGPlayMusic__

#define __GameSanGuo__SGPlayMusic__

#include <iostream>

#include "SimpleAudioEngine.h"

#include "cocos2d.h"

using namespace cocos2d;

using namespace CocosDenshion;

class SGPlayMusic

{

private:

float m_nMusicVolume;

float m_nEffectVolume;

public:

SGPlayMusic();

~SGPlayMusic();

static SGPlayMusic *sharePlayMusic();

//播放音乐

void playerMusicByType(const char* musicName);

//停止音乐

void stopMusic();

//改变音乐音量

void setVolume(float volume);

//暂停背景音乐

void pauseMusic();

//重新播放背景音乐

void rewindMusic();

//播放音效

void playEffectByType(bool isLoop, const char *effectName);

//停止音效

void stopEffect();

//改变音效音量

void setEffectVolume(float volume);

//停止某个音效

void stopEffect(unsigned int effectId);

//重新播放音效

void rewindEffect(unsigned int effectId);

};

#endif /* defined(__GameSanGuo__SGPlayMusic__) */

</span>

[cpp] view
plaincopy

<span style="font-size:18px;">//

// SGPlayMusic.cpp

// GameSanGuo

//

// Created by 江南岸 on 13-5-16.

//

//

#include "SGPlayMusic.h"

static SGPlayMusic *playMusic = NULL;

SGPlayMusic::SGPlayMusic()

{

m_nMusicVolume = 0.5;

m_nEffectVolume = 0.5f;

}

SGPlayMusic::~SGPlayMusic()

{

//卸载

SimpleAudioEngine::sharedEngine()->end();

}

SGPlayMusic* SGPlayMusic::sharePlayMusic()

{

if(NULL == playMusic)

{

playMusic = new SGPlayMusic();

}

return playMusic;

}

#pragma mark - ------ 播放音乐 -------

void SGPlayMusic::playerMusicByType(const char* musicName)

{

SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(musicName));

SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(musicName), true);

}

void SGPlayMusic::setVolume(float volume)

{

m_nMusicVolume = volume;

SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(m_nMusicVolume);

}

void SGPlayMusic::stopMusic()

{

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();

}

void SGPlayMusic::rewindMusic()

{

SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();

}

void SGPlayMusic::pauseMusic()

{

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

}

#pragma mark - ------ 播放音效 ------

void SGPlayMusic::playEffectByType(bool isLoop, const char *effectName)

{

SimpleAudioEngine::sharedEngine()->preloadEffect(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(effectName));

SimpleAudioEngine::sharedEngine()->playEffect(effectName, isLoop);

}

void SGPlayMusic::stopEffect()

{

SimpleAudioEngine::sharedEngine()->stopAllEffects();

}

void SGPlayMusic::setEffectVolume(float volume)

{

m_nEffectVolume = volume;

SimpleAudioEngine::sharedEngine()->setEffectsVolume(m_nEffectVolume);

}

void SGPlayMusic::stopEffect(unsigned int effectId)

{

SimpleAudioEngine::sharedEngine()->stopEffect(effectId);

}

void SGPlayMusic::rewindEffect(unsigned int effectId)

{

SimpleAudioEngine::sharedEngine()->resumeEffect(effectId);

}

</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: