Cocos2d-x之简单数据存储 — Userdefault

http://www.areskill.com/?p=527

1. 基本概念

本篇文章是介绍简单数据存储的Userdefault类,在API中:

1

就是存储一些简单的数据,比如声音的开启关闭,音效的开启关闭,最高分,金币数量的存储这些东西。

2. 获取

这些都是基于一些基本类型的,比如int,float,double,bool,string,二进制 这些东西,

对于它们的存取有相应的 set和 get函数,比如:

找到Userdefault.h

bool    getBoolForKey(const char* pKey); 
int     getIntegerForKey(const char* pKey);  
float   getFloatForKey(const char* pKey);  
double  getDoubleForKey(const char* pKey);  
std::string getStringForKey(const char* pKey);  
Data getDataForKey(const char* pKey);// 二进制

参数里的 pKey 就是,你在设置的时候,设置的名称,类似于一个地址,通过这个地址,你可以找到这里存储的东西。 不光这样,其实里面都还有带两个参数的,比如:

bool    getBoolForKey(const char* pKey, bool defaultValue);

其他的和这个也一样,参数有两个的,都是defaultValue,只是类型不一样而已,它的含义就是,如果你通过pKey查找不到,里面没有值,你就可以让它返回defaultValue值,并且设置成defaultValue值。

其实,如果没有defaultValue也一样,只是设置并返回的是0而已:

int UserDefault::getIntegerForKey(const char* pKey)  
{  
    return getIntegerForKey(pKey, 0);  
}  
   
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)  
{  
    const char* value = nullptr;  
    tinyxml2::XMLElement* rootNode;  
    tinyxml2::XMLDocument* doc;  
    tinyxml2::XMLElement* node;  
    node =  getXMLNodeForKey(pKey, &rootNode, &doc);  
    // find the node  
    if (node && node->FirstChild())  
    {  
        value = (const char*)(node->FirstChild()->Value());  
    }  
    int ret = defaultValue;  
    if (value)  
    {  
        ret = atoi(value);  
    }  
    if(doc)  
    {  
        delete doc;  
    }  
    return ret;  
}

上面那个是UserDefault类中两个关于int的get函数设置,可以看到没有defaultValue参数的,执行的是将defaultValue设置为0的函数。

3.设置

相应的对于set来说,就很简单了,只是设置值而已。

void    setBoolForKey(const char* pKey, bool value);  
void    setIntegerForKey(const char* pKey, int value);  
void    setFloatForKey(const char* pKey, float value);  
void    setDoubleForKey(const char* pKey, double value);  
void    setStringForKey(const char* pKey, const std::string & value);  
void    setDataForKey(const char* pKey, const Data& value);

4.关于flush

然后就是这个flush函数了,这个函数,话说我没怎么搞太懂,网上也众说纷纭。API上说是将内容保存到XML文件中,而我查了.cpp中的定义是:

void UserDefault::flush()  
{  
}

没错,就是这样,而且,在我的游戏中,没有用flush函数,也没有什么异样出现。

5.如何使用

类的相应函数都介绍完了,接下来就是使用的问题,首先要确定文件是否存在,不存在就初始化它:

if ( !LoadBooleanFromXML(“_IS_EXISTED”))   
{  
      initUserData();               
      SaveBooleanToXML(“_IS_EXISTED”, true);  
}

5.如何使用

类的相应函数都介绍完了,接下来就是使用的问题,首先要确定文件是否存在,不存在就初始化它:

if ( !LoadBooleanFromXML(“_IS_EXISTED”))   
{  
      initUserData();               
      SaveBooleanToXML(“_IS_EXISTED”, true);  
}

然后,无论设置或者获取,格式都必须是:

CCUserDefault::sharedUserDefault()->setBoolForKey(m_Sound,true);  
CCUserDefault::sharedUserDefault()->getBoolForKey(m_Sound);

前面都要有一长串东东,所以真正在使用时,都会设置宏定义来简化它,比如:

#define GetBool CCUserDefault::sharedUserDefault()->getBoolForKey  
#define SetBool CCUserDefault::sharedUserDefault()->setBoolForKey

来源网址:http://blog.csdn.net/lttree/article/details/40897789

转载请注明:游戏cocos2d-x » Cocos2d-x之简单数据存储 — Userdefault


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值