我从大富翁里提取出来里面的wav音效文件,放到我们的游戏中以增加趣味性,仅供学习研究之用
1、修改AppDelegate.cpp文件
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
//后台暂停
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
//恢复播放
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
2、在resources 目录下新建sound文件夹,存放游戏声音文件
角色的声音文件定义到ConstUtil.h文件中,大体如下:
#define P1_DEYIDEYITIAN "sound/byelottery.wav"//拜拜 祝您中奖
#define P1_need1000 "sound/need1000.wav"//只要1000元
#define P1_select_lottery "sound/select_lottery.wav"//请圈选你要购买的彩票
#define P1_meirendejiang "sound/meirendejiang.wav"//sorry 本月份没有人得奖
#define P1_xiwangshini "sound/xiwangshini.wav"//希望下次得奖者就是您
#define P1_Speaking_00181 "sound/Speaking_00181.wav"//投资获利
#define P1_Speaking_00182 "sound/Speaking_00182.wav"//投资失败
角色相关声音大体依据如下内容进行分类定义:
//交过路费声音
//抢夺别人地块
//房屋被抢夺
//房屋被摧毁
//摧毁别人房屋
//螃蟹伤人
//看到别人住院
//收取过路费
//升级房子
//不交过路费
//买地
//捡到珍珠
//对方被罚收税
例如:角色1的文件定义
//交过路费声音
#define P1_Speaking_00435 "sound/Speaking_00435.wav"//oh 哈利路亚
#define P1_Speaking_00461 "sound/Speaking_00461.wav"//oh 我的血汗钱
#define P1_Speaking_00475 "sound/Speaking_00475.wav"//算了算了 老子有的是钱
#define P1_Speaking_01060 "sound/Speaking_01060.wav"//老本都快没了
#define P1_Speaking_001062 "sound/Speaking_001062.wav"//拿去了不用找了
//抢夺别人地块
#define P1_Speaking_00429 "sound/Speaking_00429.wav"//让我把他据为己有
//房屋被抢夺
#define P1_Speaking_00430 "sound/Speaking_00430.wav"//黄金地段 让给你
#define P1_Speaking_00464 "sound/Speaking_00464.wav"//太不给面子了
#define P1_Speaking_00469 "sound/Speaking_00469.wav"//你皮子痒啊
#define P1_Speaking_00470 "sound/Speaking_00470.wav"//竟敢在太岁头上动土
#define P1_Speaking_00476 "sound/Speaking_00476.wav"//算你狠
//房屋被摧毁
#define P1_Speaking_00462 "sound/Speaking_00462.wav"//好大的胆子
#define P1_Speaking_00463 "sound/Speaking_00463.wav"//谁敢动我的地
#define P1_Speaking_00466 "sound/Speaking_00466.wav"//竟敢破坏我的好事
#define P1_Speaking_00468 "sound/Speaking_00468.wav"//拆的还真干净
#define P1_Speaking_00474 "sound/Speaking_00474.wav"//你有没有搞错啊
#define P1_Speaking_001061 "sound/Speaking_001061.wav"//真没良心
//摧毁别人房屋
#define P1_Speaking_00433 "sound/Speaking_00433.wav"//不必谢我
#define P1_Speaking_00437 "sound/Speaking_00437.wav"//全部夷为平地
//螃蟹伤人
#define P1_Speaking_00449 "sound/Speaking_00449.wav"//快来帮我把
#define P1_Speaking_01054 "sound/Speaking_01054.wav"//我惨了
#define P1_Speaking_01055 "sound/Speaking_01055.wav"//哎呦喂啊
#define P1_Speaking_001071 "sound/Speaking_001071.wav"//我不要打针
//看到别人住院
#define P1_Speaking_001073 "sound/Speaking_001073.wav"//别闹了
//收取过路费
#define P1_Speaking_00453 "sound/Speaking_00453.wav"//小本经营 概不赊欠
#define P1_Speaking_01059 "sound/Speaking_01059.wav"//蝇头小利
#define P1_Speaking_01057 "sound/Speaking_01057.wav"//这是我应得的
//升级房子
#define P1_Speaking_01051 "sound/Speaking_01051.wav"//别嫉妒我
#define P1_Speaking_001066 "sound/Speaking_001066.wav"//我真佩服自己
//不交过路费
#define P1_Speaking_00446 "sound/Speaking_00446.wav"//有钱也不给你
#define P1_Speaking_00477 "sound/Speaking_00477.wav"//可别想占我便宜啊
//买地
#define P1_Speaking_00458 "sound/Speaking_00458.wav"//盖什么好呢
#define P1_Speaking_001067 "sound/Speaking_001067.wav"//我是个大地主
//捡到珍珠
#define P1_Speaking_01052 "sound/Speaking_01052.wav"//鸿运当头
#define P1_Speaking_001063 "sound/Speaking_001063.wav"//上帝保佑
//对方被罚收税
#define P1_Speaking_00452 "sound/Speaking_00452.wav"//别想偷漏税
3、
根据声音的分类把文件名称放入到Vector中,然后根据场景随机从Vector中取出声音进行播放。
在GameBaseScene.cpp的initAudioEffect方法中,据声音的分类把文件名称放入到Vector中
void GameBaseScene::initAudioEffect()
{
.........
player2EffectVec_1.pushBack(String::create(P2_SPEAKING01));
player2EffectVec_1.pushBack(String::create(P2_QISIWOLE));
player2EffectVec_1.pushBack(String::create(P2_XINHAOKONGA));
player2EffectVec_1.pushBack(String::create(P2_BUHUIBA));
player2EffectVec_1.pushBack(String::create(P2_PAYHIGH));
player2EffectVec_1.pushBack(String::create(P2_QIANGQIANA));
player2EffectVec_1.pushBack(String::create(P2_HEBAOCHUXIE));
..........
}
在Util.cpp中定义声音播放方法,根据声音开关设置,进行声音的播放
void Util::playAudioEffect(const char* effectName,bool isLoop)
{
bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);
if(music_on)
{
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(effectName, isLoop);
}
}
//随机从Vector中取出声音进行播放
void Util::playAudioEffectRandom(Vector<String*> effectVec, bool isLoop)
{
playAudioEffect(effectVec.at(rand() % effectVec.size())->getCString(), isLoop);
}
void Util::stopAudioPlay()
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
}
4、添加背景音乐,共3首背景音乐,随机播放
void GameBaseScene::initAudioEffect()
{
bgMusicVector.push_back(BG01_MP3);
bgMusicVector.push_back(BG02_MP3);
bgMusicVector.push_back(BG03_MP3);
for (int i = 0; i<bgMusicVector.size(); i++)
{
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(bgMusicVector.at(i));
}
this->schedule(schedule_selector(GameBaseScene::playerBgMusic), 5.0f);
.....
}
5、角色对话相关的音效,就是根据具体场景,添加相应的音效播放就可以了
关于音乐音效的比较简单,可以参考 http://cn.cocos2d-x.org/tutorial/show?id=2448
这篇文章http://cn.cocos2d-x.org/tutorial/show?id=2352 ,里面的转盘界面效果做得挺好,稍微修改一下,拿到我们大富翁中来吧。
代码同下一节一并发布.