zoukankan      html  css  js  c++  java
  • 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)));
    
    
        //Okbutton
    	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是用户点击音效开发button时候回调函数,当中第④行代码是推断是否button状态从Off -> On,假设是则将开关变量isEffect 设置为false,否则为true。并且通过第⑤行代码SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav")播放一次音效。
    代码第⑥行menuMusicToggleCallback是用户点击背景音乐button时候回调函数,当中第⑦行代码是推断是否button状态从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课堂微信公共平台


  • 相关阅读:
    树上路径(树链剖分)
    P4178 Tree(点分治)
    P2146 [NOI2015] 软件包管理器(树链剖分)
    P1903 [国家集训队]数颜色 / 维护队列 (带修莫队)
    poj1182 食物链(带权并查集)
    poj3026 Borg Maze(bfs+prim)
    最佳牛围栏(二分)
    串(dp)
    POJ1258 Agri-Net
    POJ2031 Building a Space Station(prim)
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7270018.html
Copyright © 2011-2022 走看看