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

时间:2023-02-08 10:27:13
设置场景(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()函数中停止播放背景音乐。



更多内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
本书交流讨论网站: http://www.cocoagame.net
更多精彩视频课程请关注智捷课堂Cocos课程: http://v.51work6.com
欢迎加入Cocos2d-x技术讨论群:257760386


《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:

京东:http://item.jd.com/11584534.html

亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当:http://product.dangdang.com/23606265.html

互动出版网:http://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源码及样章下载地址:

源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台
Cocos2d-x实例:设置背景音乐与音效-设置场景实现