首页 > 代码库 > Cocos2d-x又一种背景音乐设置方法

Cocos2d-x又一种背景音乐设置方法

设置按钮


点一下ON切换到OFF,点一下OFF切换到ON,实现:

创建菜单按钮

	setimage = CCMenuItemImage::create("sound-on-A.png", "sound-off-A.png", this, menu_selector(Game::SetMusic));
	setimage->setScale(0.5f);
	setimage->setPosition(ccp(size.width-20,  size.height-20));
	MUISC = false;
	CCMenu *menu2 = CCMenu::create(setimage, NULL);
	menu2->setPosition(CCPointZero);
	this->addChild(menu2);

按钮回调函数:

void Game::SetMusic(CCObject* Object)
{
	if (!MUISC)//禁音 ,off图标显示
	{
		setimage->setNormalImage(CCSprite::create("sound-off-A.png"));//off可用
		setimage->setDisabledImage(CCSprite::create("sound-on-A.png"));//on不可用
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		MUISC = true;
		CCLOG("stop...");
	}
	else	//开启, on图标显示
	{
		setimage->setNormalImage(CCSprite::create("sound-on-A.png"));//on可用
		setimage->setDisabledImage(CCSprite::create("sound-off-A.png"));//off不可用
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("MainMenu.mp3", true);
		MUISC = false;
		CCLOG("start...");
	}
}