本来是打算继续写《学习篇》的,可一想,都快过年了,新年新气象嘛,要不换个章篇继续high?说干就干!
从学习篇到进阶篇,虽然还是菜鸟,但是菜鸟确实也在进步着!
这篇就讲在游戏中添加背景音乐跟音效。其实这个在-x 的testcpp 的cocosDenshionTest 里都写的很详细。
一:在.cpp中添加头文件
//cpp with cocos2d-x
#include"SimpleAudioEngine.h"
二:在init()里添加背景音乐
//cpp with cocos2d-x
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(
"background-music-aac.wav",true);
三:添加特效:
//cpp with cocos2d-x
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(
"pew-pew-lei.wav");
四:还有一些其他操作,包括pause,stop等:
backGround:
// stop background music
case 1:
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
break;
// pause background music
case 2:
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
break;
// resume background music
case 3:
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
break;
// rewind background music
case 4:
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
break;
Effect:
// stop effect
case 8:
SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
break;
// unload effect
case 9:
SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
break;
// add bakcground music volume
case 10:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
break;
// sub backgroud music volume
case 11:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
break;
// add effects volume
case 12:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
break;
// sub effects volume
case 13:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
break;
case 14:
SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
break;
case 15:
SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
break;
case 16:
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
break;
case 17:
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
break;
case 18:
SimpleAudioEngine::sharedEngine()->stopAllEffects();
五:注意!
1、音乐格式要注意,像在pc机上wav,mp3是可以用的,ogg那是肯定不可以的。
2、开始播放音乐后,即使你切换场景,音乐还是在播放。所以需要在切换场景的同时把音乐关上。