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

本文转载来源:http://blog.csdn.net/lttree/article/details/40897789


1.基本概念

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


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




2.获取

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

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

找到Userdefault.h

[cpp]  view plain  copy
 print ?
  1. bool    getBoolForKey(const char* pKey);<span style="white-space:pre">        </span>  
  2. int     getIntegerForKey(const char* pKey);  
  3. float   getFloatForKey(const char* pKey);  
  4. double  getDoubleForKey(const char* pKey);  
  5. std::string getStringForKey(const char* pKey);  
  6. Data getDataForKey(const char* pKey);<span style="white-space:pre">       </span>// 二进制  

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

[cpp]  view plain  copy
 print ?
  1. bool    getBoolForKey(const char* pKey, bool defaultValue);  

其他的和这个也一样,参数有两个的,都是defaultValue,只是类型不一样而已,

它的含义就是,如果你通过pKey查找不到,里面没有值,你就可以让它返回defaultValue值,并且设置成defaultValue值。

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

[cpp]  view plain  copy
 print ?
  1. <span style="font-family:Comic Sans MS;font-size:14px;">int UserDefault::getIntegerForKey(const char* pKey)  
  2. {  
  3.     return getIntegerForKey(pKey, 0);  
  4. }  
  5.   
  6. int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)  
  7. {  
  8.     const char* value = nullptr;  
  9.     tinyxml2::XMLElement* rootNode;  
  10.     tinyxml2::XMLDocument* doc;  
  11.     tinyxml2::XMLElement* node;  
  12.     node =  getXMLNodeForKey(pKey, &rootNode, &doc);  
  13.     // find the node  
  14.     if (node && node->FirstChild())  
  15.     {  
  16.         value = (const char*)(node->FirstChild()->Value());  
  17.     }  
  18.     int ret = defaultValue;  
  19.     if (value)  
  20.     {  
  21.         ret = atoi(value);  
  22.     }  
  23.     if(doc)  
  24.     {  
  25.         delete doc;  
  26.     }  
  27.     return ret;  
  28. }</span>  

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




3.设置

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

[cpp]  view plain  copy
 print ?
  1. void    setBoolForKey(const char* pKey, bool value);  
  2. void    setIntegerForKey(const char* pKey, int value);  
  3. void    setFloatForKey(const char* pKey, float value);  
  4. void    setDoubleForKey(const char* pKey, double value);  
  5. void    setStringForKey(const char* pKey, const std::string & value);  
  6. void    setDataForKey(const char* pKey, const Data& value);  




4.关于flush

然后就是这个flush函数了,

这个函数,话说我没怎么搞太懂,网上也众说纷纭,

API上说是 将内容保存到XML文件中,

而我查了.cpp中的定义是:

[cpp]  view plain  copy
 print ?
  1. void UserDefault::flush()  
  2. {  
  3. }  

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




5.如何使用

类的相应函数都介绍完了,接下来就是使用的问题,

首先要确定文件是否存在,不存在就初始化它:

[cpp]  view plain  copy
 print ?
  1. if ( !LoadBooleanFromXML("_IS_EXISTED"))   
  2. {  
  3.        initUserData();               
  4.        SaveBooleanToXML("_IS_EXISTED"true);  
  5. }  

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

[cpp]  view plain  copy
 print ?
  1. CCUserDefault::sharedUserDefault()->setBoolForKey(m_Sound,true);  
  2. CCUserDefault::sharedUserDefault()->getBoolForKey(m_Sound);  

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

[cpp]  view plain  copy
 print ?
  1. #define GetBool CCUserDefault::sharedUserDefault()->getBoolForKey  
  2. #define SetBool CCUserDefault::sharedUserDefault()->setBoolForKey  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值