一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。
Audio.h
#ifndef _AUdio_H_ #define _AUdio_H_ class Audio{ public: static Audio* getInstance(); void playBGM(); void playReadyGo(); void playPop(); void prepare(); void playCombo(int size); private: static Audio* m_instance; }; #endif
Audio.cpp
#include "Audio.h" #include "SimpleAudioEngine.h" Audio* Audio::m_instance = nullptr; Audio* Audio::getInstance(){ if(m_instance == nullptr){ m_instance = new Audio(); } return m_instance; } void Audio::playBGM(){ CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("Music/music.ogg",true); } void Audio::playReadyGo(){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Music/readygo.ogg",false); } void Audio::playPop(){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Music/pop.ogg",false); } void Audio::playCombo(int size){ if(size < 5) return; if(size >= 10){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Music/combo_3.ogg",false); }else if(size >= 7){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Music/combo_2.ogg",false); }else { CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("Music/combo_1.ogg",false); } } void Audio::prepare(){ CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("Music/music.ogg"); CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("Music/readygo.ogg"); CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("Music/pop.ogg"); CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("Music/combo_1.ogg"); CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("Music/combo_2.ogg"); CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("Music/combo_3.ogg"); }
好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。
最后附上整个项目的源代码
原文链接:/cocos2dx/347191.html