CCControlSwitch
Cocos2D-x 用CCControlSwitch设置音乐的开关
在 .h 当中定义下面这个方法
void callbackSwitch(CCObject *ksender, CCControlEvent controlEvent);
////////////////////////////////////////////////
.cpp当中
CCControlSwitch *switchControl = CCControlSwitch::create
(
CCSprite::create("switch-mask.png"), //switch开关的外框
CCSprite::create("switch-on.png"), //switch开关处于开的位置
CCSprite::create("switch-off.png"), //switch开关处于关的位置
CCSprite::create("switch-thumb.png"), //标志的小圆点
CCLabelTTF::create("On", "Arial-BoldMT", 16), // 设置开关里面显示为 " On "
CCLabelTTF::create("Off", "Arial-BoldMT", 16) // 设置开关里面显示为 " Off "
);
switchControl->setPosition(ccp (100, 300)); // 设置坐标
switchControl->setTouchEnabled(true);
//添加回调函数
switchControl->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::callbackSwitch), CCControlEventValueChang
callbackSwitch(switchControl, CCControlEventValueChanged);
addChild(switchControl);
/////////////////////////////////////////////////////////////////
//下面就是实现方法
void HelloWorld::callbackSwitch(CCObject* ksender, CCControlEvent controlEvent)
{
CCControlSwitch* pSwitch = (CCControlSwitch*)ksender;
if (pSwitch->isOn())
{
//继续背景音乐播放
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
CCLog("CCControlSwitch value = ON");
}
else
{
//暂停背景音乐播放
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
CCLog("CCControlSwitch value = OFF");
}
}
备注:
与上一篇 "Cocos2D-x 用CCControlSlider设置调节音量的大小" 结合在一起就可以做游戏或应用的音乐设置模块了