Cocos2d-x 3.2 大富翁游戏项目开发-第二十九部分 游戏配音

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第二十九部分 游戏配音前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我从大富翁里提取出来里面的wav音效文件,放到我们的游戏中以增加趣味性,仅供学习研究之用

1、修改AppDelegate.cpp文件

  1. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  2. void AppDelegate::applicationDidEnterBackground() {
  3. Director::getInstance()->stopAnimation();
  4.  
  5. // if you use SimpleAudioEngine,it must be pause
  6. //后台暂停
  7. SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  8. }
  9.  
  10. // this function will be called when the app is active again
  11. void AppDelegate::applicationWillEnterForeground() {
  12. Director::getInstance()->startAnimation();
  13.  
  14. // if you use SimpleAudioEngine,it must resume here
  15. //恢复播放
  16. SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  17. }

2、在resources 目录下新建sound文件夹,存放游戏声音文件

角色的声音文件定义到ConstUtil.h文件中,大体如下:
  1. #define P1_DEYIDEYITIAN "sound/byelottery.wav"//拜拜 祝您中奖
  2. #define P1_need1000 "sound/need1000.wav"//只要1000元
  3. #define P1_select_lottery "sound/select_lottery.wav"//请圈选你要购买的彩票
  4. #define P1_meirendejiang "sound/meirendejiang.wav"//sorry 本月份没有人得奖
  5. #define P1_xiwangshini "sound/xiwangshini.wav"//希望下次得奖者就是您
  6. #define P1_Speaking_00181 "sound/Speaking_00181.wav"//投资获利
  7. #define P1_Speaking_00182 "sound/Speaking_00182.wav"//投资失败

角色相关声音大体依据如下内容进行分类定义:
//交过路费声音
//抢夺别人地块
//房屋被抢夺
//房屋被摧毁
//摧毁别人房屋
//螃蟹伤人
//看到别人住院
//收取过路费
//升级房子
//不交过路费
//买地
//捡到珍珠
//对方被罚收税


例如:角色1的文件定义
  1. //交过路费声音
  2. #define P1_Speaking_00435 "sound/Speaking_00435.wav"//oh 哈利路亚
  3. #define P1_Speaking_00461 "sound/Speaking_00461.wav"//oh 我的血汗钱
  4. #define P1_Speaking_00475 "sound/Speaking_00475.wav"//算了算了 老子有的是钱
  5. #define P1_Speaking_01060 "sound/Speaking_01060.wav"//老本都快没了
  6. #define P1_Speaking_001062 "sound/Speaking_001062.wav"//拿去了不用找了
  7. //抢夺别人地块
  8. #define P1_Speaking_00429 "sound/Speaking_00429.wav"//让我把他据为己有
  9. //房屋被抢夺
  10. #define P1_Speaking_00430 "sound/Speaking_00430.wav"//黄金地段 让给你
  11. #define P1_Speaking_00464 "sound/Speaking_00464.wav"//太不给面子了
  12. #define P1_Speaking_00469 "sound/Speaking_00469.wav"//你皮子痒啊
  13. #define P1_Speaking_00470 "sound/Speaking_00470.wav"//竟敢在太岁头上动土
  14. #define P1_Speaking_00476 "sound/Speaking_00476.wav"//算你狠
  15. //房屋被摧毁
  16. #define P1_Speaking_00462 "sound/Speaking_00462.wav"//好大的胆子
  17. #define P1_Speaking_00463 "sound/Speaking_00463.wav"//谁敢动我的地
  18. #define P1_Speaking_00466 "sound/Speaking_00466.wav"//竟敢破坏我的好事
  19. #define P1_Speaking_00468 "sound/Speaking_00468.wav"//拆的还真干净
  20. #define P1_Speaking_00474 "sound/Speaking_00474.wav"//你有没有搞错啊
  21. #define P1_Speaking_001061 "sound/Speaking_001061.wav"//真没良心
  22. //摧毁别人房屋
  23. #define P1_Speaking_00433 "sound/Speaking_00433.wav"//不必谢我
  24. #define P1_Speaking_00437 "sound/Speaking_00437.wav"//全部夷为平地
  25. //螃蟹伤人
  26. #define P1_Speaking_00449 "sound/Speaking_00449.wav"//快来帮我把
  27. #define P1_Speaking_01054 "sound/Speaking_01054.wav"//我惨了
  28. #define P1_Speaking_01055 "sound/Speaking_01055.wav"//哎呦喂啊
  29. #define P1_Speaking_001071 "sound/Speaking_001071.wav"//我不要打针
  30. //看到别人住院
  31. #define P1_Speaking_001073 "sound/Speaking_001073.wav"//别闹了
  32. //收取过路费
  33. #define P1_Speaking_00453 "sound/Speaking_00453.wav"//小本经营 概不赊欠
  34. #define P1_Speaking_01059 "sound/Speaking_01059.wav"//蝇头小利
  35. #define P1_Speaking_01057 "sound/Speaking_01057.wav"//这是我应得的
  36. //升级房子
  37. #define P1_Speaking_01051 "sound/Speaking_01051.wav"//别嫉妒我
  38. #define P1_Speaking_001066 "sound/Speaking_001066.wav"//我真佩服自己
  39. //不交过路费
  40. #define P1_Speaking_00446 "sound/Speaking_00446.wav"//有钱也不给你
  41. #define P1_Speaking_00477 "sound/Speaking_00477.wav"//可别想占我便宜啊
  42. //买地
  43. #define P1_Speaking_00458 "sound/Speaking_00458.wav"//盖什么好呢
  44. #define P1_Speaking_001067 "sound/Speaking_001067.wav"//我是个大地主
  45. //捡到珍珠
  46. #define P1_Speaking_01052 "sound/Speaking_01052.wav"//鸿运当头
  47. #define P1_Speaking_001063 "sound/Speaking_001063.wav"//上帝保佑
  48. //对方被罚收税
  49. #define P1_Speaking_00452 "sound/Speaking_00452.wav"//别想偷漏税

3、
根据声音的分类文件名称放入到Vector中,然后根据场景随机从Vector中取出声音进行播放。

在GameBaseScene.cpp的initAudioEffect方法中,据声音的分类文件名称放入到Vector中
  1. void GameBaseScene::initAudioEffect()
  2. {
  3. .........
  4. player2EffectVec_1.pushBack(String::create(P2_SPEAKING01));
  5. player2EffectVec_1.pushBack(String::create(P2_QISIWOLE));
  6. player2EffectVec_1.pushBack(String::create(P2_XINHAOKONGA));
  7. player2EffectVec_1.pushBack(String::create(P2_BUHUIBA));
  8. player2EffectVec_1.pushBack(String::create(P2_PAYHIGH));
  9. player2EffectVec_1.pushBack(String::create(P2_QIANGQIANA));
  10. player2EffectVec_1.pushBack(String::create(P2_HEBAOCHUXIE));
  11. ..........
  12. }
在Util.cpp中定义声音播放方法,根据声音开关设置,进行声音的播放
  1. void Util::playAudioEffect(const char* effectName,bool isLoop)
  2. {
  3. bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);
  4.  
  5. if(music_on)
  6. {
  7. CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(effectName,isLoop);
  8. }
  9. }

//随机从Vector中取出声音进行播放
  1. void Util::playAudioEffectRandom(Vector<String*> effectVec,bool isLoop)
  2. {
  3. playAudioEffect(effectVec.at(rand() % effectVec.size())->getCString(),isLoop);
  4. }
  5.  
  6. void Util::stopAudioPlay()
  7. {
  8. CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
  9. CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
  10. }

4、添加背景音乐,共3首背景音乐,随机播放
  1. void GameBaseScene::initAudioEffect()
  2. {
  3. bgMusicVector.push_back(BG01_MP3);
  4. bgMusicVector.push_back(BG02_MP3);
  5. bgMusicVector.push_back(BG03_MP3);
  6.  
  7. for (int i = 0; i<bgMusicVector.size(); i++)
  8. {
  9. CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(bgMusicVector.at(i));
  10. }
  11. this->schedule(schedule_selector(GameBaseScene::playerBgMusic),5.0f);
  12. .....
  13. }

5、角色对话相关的音效,就是根据具体场景,添加相应的音效播放就可以了


关于音乐音效的比较简单,可以参考 http://cn.cocos2d-x.org/tutorial/show?id=2448

这篇文章http://cn.cocos2d-x.org/tutorial/show?id=2352 ,里面的转盘界面效果做得挺好,稍微修改一下,拿到我们大富翁中来吧。

代码同下一节一并发布.

猜你在找的Cocos2d-x相关文章