cocos2d-x游戏中音乐音效的处理

本文详细介绍了Cocos2d-x游戏中音乐音效的处理方法,包括状态设置、使用CCUserDefault进行状态存储及解决音乐意外开启的问题。通过设置状态值及利用AppDelegate的生命周期方法,实现音乐音效的灵活控制,并解释了音乐意外开启的原因及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[i][b]本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: http://www.firedragonpzy.com.cn/index.php/archives/905[/b][/i]

有关cocos2d-x游戏中音乐音效的处理一般可选两种方式:第一种,在没有进度处理的情况下,最好将音乐音效的状态设置为三种,开状态,关状态,未初始化状态,因为得判断是否首次进入场景,然后进行处理。第二种,有了进度的情况下,设置一种即可。
接下来分别说下两种的设置,关于第一种,你可以讲状态设置为三个值,0,1,2等等,但是我是个比较懒惰的人,所以我设置了两个值,bool型,true或false。这是本文的精华,大家注意哦。
首先,大家先看看CCUserDefault的头文件,如下:
    /**
@brief Get bool value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is false.
*/
bool getBoolForKey(const char* pKey, bool defaultValue = false);
/**
@brief Get integer value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.
*/
int getIntegerForKey(const char* pKey, int defaultValue = 0);
/**
@brief Get float value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0f.
*/
float getFloatForKey(const char* pKey, float defaultValue=0.0f);
/**
@brief Get double value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0.
*/
double getDoubleForKey(const char* pKey, double defaultValue=0.0);
/**
@brief Get string value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is "".
*/
std::string getStringForKey(const char* pKey, const std::string & defaultValue = "");

如果没有设置的bool型为false,所以我讲未初始化和开状态都设置为false,而关状态设置为true。

关于第二种,设置了进度,即可在进度处设置音乐音效的开关,如下代码:
    CCUserDefault *userDefault = CCUserDefault::sharedUserDefault();
if (!userDefault->getIntegerForKey(gcCURPROGRESSMARK))
{
userDefault->setIntegerForKey(gcCURPROGRESSMARK, nBLOOMINGDALEFIRST );
userDefault->setIntegerForKey(gcHASICELOTUS,0);
userDefault->setIntegerForKey(gcHASCRYSTAL,0);
userDefault->setIntegerForKey(gcHASWINDBELL,0);
userDefault->setBoolForKey( gcMUSICSTATE, true );
userDefault->setBoolForKey( gcEFFECTSTATE, true );
}

这只是音乐音效开关的设置,但是我还遇到了一个比较扯淡的问题,我在游戏音乐设置界面,将音乐关了,按Home键退出,再次进入的时候音乐竟然是打开的,而我的音乐按钮是关闭的。音乐按钮关闭是应该的,但是音乐打开确实不对的。在退出之前我明明是将音乐关掉的啊!
若你也有此疑问,那就接着往下看。
相信很多朋友们都很少关注AppDelegate.cpp吧。其实我也关注很少,偶然发现里面有这段代码,如下:
    // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->pause();
if ( MusicManager::getMusicState() )
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
else
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->resume();
if ( MusicManager::getMusicState() )
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
else
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
}

当然我贴出来的是我修改后的代码,When comes a phone call,it's be invoked too这就是按了home键,// this function will be called when the app is active again再次进入的时候,相信到这里大家就应该很明白了吧!

提醒:(1)在使用CCUserDefault的时候注意状态值得设置,是设置两个还是三个
(2)注意AppDelegate里面的applicationDidEnterBackground()和applicationWillEnterForeground()方法,我现在知道的就是音乐要在这里处理下,别的没有必要,要是有别的需要处理,欢迎大家评论指教……
今天就先到这里吧,呼呼……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值