首页 > 代码库 > Cocos2d-x实例:设置背景音乐与音效-设置场景实现

Cocos2d-x实例:设置背景音乐与音效-设置场景实现

设置场景(Setting),Setting.h文件代码如下:
#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__




上述代码第①是定义布尔类型成员变量isEffect,它用来保存现在是否允许音效,事实上是否允许音效和背景音乐播放状态应该持久化到文件中,由于我们还没有介绍数据持久化,所以我们采用isEffect变量记录状态,当我们在后面的章节学习完成数据持久化后,再保存它们状态。其它的代码与HelloWorld场景代码非常相似,这里不再赘述。Setting.cpp文件代码如下:
#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");			⑩
}


上述代码第①、③、⑧行是在判断isEffect为true(音效播放开关打开)情况下播放音效。
代码第②行menuSoundToggleCallback是用户点击音效开发按钮时候回调函数,其中第④行代码是判断是否按钮状态从Off -> On,如果是则将开关变量isEffect 设置为false,否则为true,而且通过第⑤行代码SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav")播放一次音效。
代码第⑥行menuMusicToggleCallback是用户点击背景音乐按钮时候回调函数,其中第⑦行代码是判断是否按钮状态从Off -> On,如果是则停止播放背景音乐,否则为开始播放。

第⑧行代码是onEnterTransitionDidFinish()函数中播放背景音乐。第⑨行代码是cleanup()函数中停止播放背景音乐。



更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678