cocos2d-x 菜鸟进阶篇(一) 在游戏中添加music

本来是打算继续写《学习篇》的,可一想,都快过年了,新年新气象嘛,要不换个章篇继续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、开始播放音乐后,即使你切换场景,音乐还是在播放。所以需要在切换场景的同时把音乐关上。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值