cocos2d-x开发 之 使用CheckBox、UserDefault、CocosDenshion做声音开关。

在日常游戏开发当中,每个游戏都会有游戏开关的选项。

接下来我会使用CheckBox和UserDefault做声音开关。

一.关于UserDefault。

引擎提供的UserDefault可以简单地储存游戏中的信息,例如背景音乐音效的开关状态,用户名等等。

UserDefault是一个单例对象,可以用getInstance方法来获得。 
如果是第一次调用UserDefault::getInstance(),则会自动生成一个UserDefault.xml文件,目录在工程目录的proj.win32\Debug.win32中,用来保存所有的UserDefault中的数据,还有UserDefault中的数据是以key-value(键-值)的方式来储存的。

判断是否已经存在UserDefault.xml文件

UserDefault::isXMLFileExist();

写入数据

UserDefault::getInstance()->setStringForKey("string","value");
UserDefault::getInstance()->setIntegerForKey("int",11);
UserDefault::getInstance()->setFloatForKey("float",1.1f);
UserDefault::getInstance()->setDoubleForKey("double",1.2);
UserDefault::getInstance()->setBoolForKey("bool",false);

其中第一个参数为自己为这个数据起的名字,也就是变量名称,第二个为数据。

读取数据

std::string str = UserDefault::getInstance()->getStringForKey("string");    //根据键值取出std::string类型字符串
int i = UserDefault::getInstance()->getIntegerForKey("int");
float f = UserDefault::getInstance()->getFloatForKey("float");
double d = UserDefault::getInstance()->getDoubleForKey("double");
bool b = UserDefault::getInstance()->getBoolForKey("bool");

需要创建一个相应变量来容纳值。

二.关于CheckBox

https://blog.csdn.net/qq_42428486/article/details/91395818

三.关于CocosDenshion

https://blog.csdn.net/qq_42428486/article/details/90815316

四.开始构建

头文件

#ifndef SettingScene_h
#define SettingScene_h
#include "cocos2d.h"
using namespace cocos2d;

#include "ui/CocosGUI.h"
using namespace ui;

#include "MyMenuScene.h"

class SettingScene:public Scene
{
public:
    virtual bool init();
    CREATE_FUNC(SettingScene);
    static Scene * CreateScene();
    void comeback(Ref * temp);
    bool isMusicOnit;
private:
    CheckBox * box;//声音开关
    Label * label; //显示开关
};

#endif /* SettingScene_h */

cpp文件:

#include "SettingScene.h"
#include "MyMenuScene.h"
#include <iostream>
using namespace std;
Scene * SettingScene::CreateScene()
{
    return SettingScene::create();
}

bool SettingScene::init()
{
    if (!Scene::init())
    {
        return false;
    }
    //获取屏幕大小
    Size visitable = Director::getInstance()->getVisibleSize();
    
    //背景
    auto photo_label = Sprite::create("jm.JPG");
    photo_label->setPosition(visitable.width / 2, visitable.height / 2);
    photo_label->setScale(1);
    this->addChild(photo_label);
    
    
    //标题
    auto title = Label::createWithSystemFont("Setting", "arial.ttf",30);
    this->addChild(title);
    title->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    title->setPosition(visitable.width / 2, visitable.height / 2 +60);
    
    
    //声音开关按钮
    box = CheckBox::create("CheckBox_delected.png", "CheckBox_undelected.png");
    this->addChild(box);
    box->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    box->setPositionX(visitable.width / 2);
    box->setPositionY(visitable.height / 2);
    box->setScale(0.6);
    
   
    
    //开关
    label = Label::createWithTTF("sound down", "fonts/my.TTF", 15);
    this->addChild(label);
    label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    label->setPosition(visitable.width /2 , visitable.height /2+20);
    
    //读取配置,如果没有就创建并初始化为true
     bool music = UserDefault::getInstance()->getBoolForKey("music",true);
    
    // 根据配置来设置声音开关的状态
    if(music == true)
    {
        box->setSelected(true);
        label->setString("sound up");
        cout << "音乐开启" << endl;
    }else{
        box->setSelected(false);
        label->setString("sound down");
    }
    
    
    //加入事件
    box->addTouchEventListener([&](Ref* temp, ui::CheckBox::TouchEventType type)
                               {
                                   if (type == ui::CheckBox::TouchEventType::ENDED)
                                   {
                                       auto temp1 = dynamic_cast<CheckBox*> (temp);
                                       if (temp1->isSelected() == true) //正常状态
                                       {
                                           CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();//暂停背景音乐
                                           label->setString("sound down");                            //更改字节显示
                                           UserDefault::getInstance()->setBoolForKey("music", false); //更新声音设置为关
                                       }
                                       else  //按压状态
                                       {
                                           CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
                                           //继续背景音乐
                                           label->setString("sound up");                             //更新字节显示
                                           UserDefault::getInstance()->setBoolForKey("music",true);  //更新声音设置为开
                                       }
                                   }
                               });
    
    //返回按钮
    auto exit_label = Label::createWithSystemFont("back", "font/MarkerFelt.ttf", 15);
    auto exit_menuitem = MenuItemLabel::create(exit_label, CC_CALLBACK_1(SettingScene::comeback, this));
    auto exit_menu = Menu::create(exit_menuitem, NULL);
    exit_menu->setPosition(visitable.width / 2, visitable.height / 2 - 40);
    this->addChild(exit_menu);
    return true;
}
void SettingScene::comeback(Ref * temp)
{
    
    Director::getInstance()->popScene();//返回主场景
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值