(1)头文件中
USING_NS_CC;
USING_NS_CC_EXT;
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
class LoginLayer:public CCLayer
{
public:
virtual bool init();
SCENE_FUNC(LoginLayer);
CREATE_FUNC(LoginLayer);
//开关声音
void SoundOnOffClick(CCObject *sender);
bool isSoundOpen;
};
(2)实现文件中bool LoginLayer::init()
{
if(!CCLayer::init())
{
return false;
}
isSoundOpen=false;
//添加声音按钮
CCMenuItemImage *soundbtnOn=CCMenuItemImage::create("soundon.png", "soundon.png");
CCMenuItemImage *soundbtnOff=CCMenuItemImage::create("soundoff.png", "soundoff.png");
CCMenuItemToggle *toggle=CCMenuItemToggle::createWithTarget(this, menu_selector(LoginLayer::SoundOnOffClick),soundbtnOn,soundbtnOff,NULL);
toggle->setPosition(CCPointMake(0,0));
CCMenu *menu1=CCMenu::create(toggle,NULL);
menu1->setPosition(ccp(50,50));
this->addChild(menu1);
SimpleAudioEngine::sharedEngine()->playBackgroundMusic("yinyue.mp3", true);
return true;
}
//声音的开与关的实现
void LoginLayer::SoundOnOffClick(CCObject *sender)
{
if(isSoundOpen)
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
isSoundOpen=false;
}
else
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
isSoundOpen=true;
}
}