@H_502_0@ @H_502_0@
@H_502_0@ @H_502_0@
@H_502_0@
第二十三课:Cocos2D-X音频系统1-3
=======================================================================================================================================================================
课程目标:
-Cocos2D-X的音频系统
课程重点:
-理解音乐和音效
-Cocos2D-X中音频系统的运用
考核目标:
-能够为Cocos2D-X游戏添加音乐音效
=======================================================================================================================================================================
一、音乐和音效的使用例子
#include "AppMacros.h" #include "HelloMusic.h" #include "SimpleAudioEngine.h" #define LINE_SPACE 40 using namespace CocosDenshion; using namespace cocos2d; using namespace std; CCScene* HelloMusic::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloMusic *layer = HelloMusic::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloMusic::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } std::string testItems[] = { "play background music","stop background music","pause background music","resume background music","rewind background music","is background music playing","play effect","play effect repeatly","stop effect","unload effect","add background music volume","sub background music volume","add effects volume","sub effects volume","pause effect","resume effect","pause all effects","resume all effect","stop all effects" }; screenSize = CCDirector::sharedDirector()->getWinSize(); m_pMenu = CCMenu::create(); m_iTestCount = sizeof(testItems) / sizeof(testItems[0]); //循环创建菜单项 for (int i=0; i<m_iTestCount; i++) { CCLabelTTF* label = CCLabelTTF::create(testItems[i].c_str(),"Arial",24); CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label,this,menu_selector(HelloMusic::menuCallback)); m_pMenu->addChild(pMenuItem,i+1000); pMenuItem->setPosition( ccp( screenSize.width/2,screenSize.height-(i+1)* LINE_SPACE)); } m_pMenu->setContentSize(CCSizeMake(screenSize.width,(m_iTestCount +1 ) * LINE_SPACE)); m_pMenu->setPosition(CCPointZero); addChild(m_pMenu); setTouchEnabled(true); //预加载音乐和音效 std::string music = "background.mp3"; std::string effect1 = "effect1.wav"; SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(music.c_str());//false:表示不循环播放 SimpleAudioEngine::sharedEngine()->preloadEffect(effect1.c_str()); //调节音乐和音效的大小 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5f); SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5f); return true; } //退出时关闭声音 void HelloMusic::onExit() { CCLayer::onEnter(); SimpleAudioEngine::sharedEngine()->end(); } void HelloMusic::menuCallback(CCObject* pSender) { CCMenuItem* pMenuItem = (CCMenuItem*)(pSender); int iIdex = pMenuItem->getZOrder() - 1000; string music = "background.mp3"; string effect1 = "effect1.wav"; //取出菜单项索引 switch( iIdex ) { case 0: SimpleAudioEngine::sharedEngine()->playBackgroundMusic(music.c_str(),true);//false:表示不循环播放 break; case 1: SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); break; case 2: SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); break; case 3: SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); break; case 4: SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic(); break; case 5: if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) { CCLOG("background music is playing"); } else { CCLOG("background music is not playing"); } break; //特效操作 case 6: //可能同时播放多个特效,通过ID停止对应的音效 m_iSoundId = SimpleAudioEngine::sharedEngine()->playEffect(effect1.c_str(),false); break; case 7: m_iSoundId = SimpleAudioEngine::sharedEngine()->playEffect(effect1.c_str(),true); break; case 8: SimpleAudioEngine::sharedEngine()->stopEffect(m_iSoundId); break; case 9: SimpleAudioEngine::sharedEngine()->unloadEffect(effect1.c_str()); break; case 10: SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f); break; case 11: SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f); break; case 12: SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f); break; case 13: SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f); break; case 14: SimpleAudioEngine::sharedEngine()->pauseEffect(m_iSoundId); break; case 15: SimpleAudioEngine::sharedEngine()->resumeEffect(m_iSoundId); break; case 16: SimpleAudioEngine::sharedEngine()->pauseAllEffects(); break; case 17: SimpleAudioEngine::sharedEngine()->resumeAllEffects(); break; case 18: SimpleAudioEngine::sharedEngine()->stopAllEffects(); break; } } void HelloMusic::ccTouchesBegan(CCSet* pTouches,CCEvent* pEvent) { //获取第一个点 CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); //转换成本地的点 m_iBeginPos = touch->getLocation(); } void HelloMusic::ccTouchesMoved(CCSet* pTouches,CCEvent* pEvent) { //获取第一个点 CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); //转换成本地的点 CCPoint touchLocation = touch->getLocation(); float iMoveY = touchLocation.y - m_iBeginPos.y; CCPoint curPos = m_pMenu->getPosition(); CCPoint nextPos = ccp(curPos.x,curPos.y + iMoveY); if (nextPos.y < 0.0f) { m_pMenu->setPosition(CCPointZero); return; } if (nextPos.y > (m_iTestCount+1)*LINE_SPACE - screenSize.height) { m_pMenu->setPosition(ccp( 0,(m_iTestCount + 1) * LINE_SPACE - screenSize.height )); return; } // CCPoint curPos1 = m_pMenu->getPosition(); // CCLOG("curPosx=%d",curPos1.x); // CCLOG("curPosy=%d",curPos1.y); m_pMenu->setPosition(nextPos); m_iBeginPos = touchLocation; }
===================================================================
总结:
音乐系统还是容易掌握滴。
开心一刻:
问兄弟:“你觉得我长得怎么样?说实话!!”
他想了想回答说:“嗯,挺阳光的~~”
我:“我就说嘛!我长得还不赖。。。”
他:“就是有点刺眼!
【麦可网】Cocos2d-X跨平台游戏开发---教程下载:http://pan.baidu.com/s/1kTio1Av
【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu