#ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include "cocos2d.h" #include "SimpleAudioEngine.h" class Setting : public cocos2d::Layer { bool isEffect; ① public: static cocos2d::Scene* createScene(); virtual bool init(); virtual void onEnter(); virtual void onEnterTransitionDidFinish(); virtual void onExit(); virtual void onExitTransitionDidStart(); virtual void cleanup(); void menuSoundToggleCallback(cocos2d::Ref* pSender); void menuMusicToggleCallback(cocos2d::Ref* pSender); void menuOkCallback(cocos2d::Ref* pSender); CREATE_FUNC(Setting); }; #endif // __Setting_SCENE_H__ #define __HELLOWORLD_SCENE_H__
#include "SettingScene.h" USING_NS_CC; using namespace CocosDenshion; … … bool Setting::init() { if ( !Layer::init() ) { return false; } log("Setting init"); Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Sprite *bg = Sprite::create("setting-back.png"); bg->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height /2)); this->addChild(bg); //音效 auto soundOnMenuItem = MenuItemImage::create( "on.png", "on.png"); auto soundOffMenuItem = MenuItemImage::create( "off.png", "off.png"); auto soundToggleMenuItem = MenuItemToggle::createWithCallback( CC_CALLBACK_1(Setting::menuSoundToggleCallback, this), soundOffMenuItem, soundOnMenuItem, NULL); soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 220))); //背景音乐 auto musicOnMenuItem = MenuItemImage::create( "on.png", "on.png"); auto musicOffMenuItem = MenuItemImage::create( "off.png", "off.png"); auto musicToggleMenuItem = MenuItemToggle::createWithCallback( CC_CALLBACK_1(Setting::menuMusicToggleCallback, this), musicOffMenuItem, musicOnMenuItem, NULL ); musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 362))); //Ok按钮 auto okMenuItem = MenuItemImage::create( "ok-down.png", "ok-up.png", CC_CALLBACK_1(Setting::menuOkCallback, this)); okMenuItem->setPosition(Director::getInstance()->convertToGL(Point(600, 510))); Menu* mn = Menu::create(soundToggleMenuItem, musicToggleMenuItem,okMenuItem, NULL); mn->setPosition(Point::ZERO); this->addChild(mn); return true; } void Setting::menuOkCallback(Ref* pSender) { Director::getInstance()->popScene(); if (isEffect) { SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ① } } void Setting::menuSoundToggleCallback(Ref* pSender) ② { auto soundToggleMenuItem = (MenuItemToggle*)pSender; log("soundToggleMenuItem %d", soundToggleMenuItem->getSelectedIndex()); if (isEffect) { SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ③ } if (soundToggleMenuItem->getSelectedIndex() == 1) {//选中状态Off -> On ④ isEffect = false; } else { isEffect = true; SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ⑤ } } void Setting::menuMusicToggleCallback(Ref* pSender) ⑥ { auto musicToggleMenuItem = (MenuItemToggle*)pSender; log("musicToggleMenuItem %d", musicToggleMenuItem->getSelectedIndex()); if (musicToggleMenuItem->getSelectedIndex() == 1) {//选中状态Off -> On ⑦ SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Synth.mp3"); } else { SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3"); } if (isEffect) { SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ⑧ } } void Setting::onEnter() { Layer::onEnter(); log("Setting onEnter"); } void Setting::onEnterTransitionDidFinish() { Layer::onEnterTransitionDidFinish(); log("Setting onEnterTransitionDidFinish"); isEffect = true; //播放 SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Synth.mp3", true); ⑨ } void Setting::onExit() { Layer::onExit(); log("Setting onExit"); } void Setting::onExitTransitionDidStart() { Layer::onExitTransitionDidStart(); log("Setting onExitTransitionDidStart"); } void Setting::cleanup() { Layer::cleanup(); log("Setting cleanup"); //停止 SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Synth.mp3"); ⑩ }
第⑧行代码是onEnterTransitionDidFinish()函数中播放背景音乐。第⑨行代码是cleanup()函数中停止播放背景音乐。
Cocos2d-x实例:设置背景音乐与音效-设置场景实现,布布扣,bubuko.com
原文:http://blog.csdn.net/tonny_guan/article/details/38389827