码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-07 22:07:55      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   简单数据存储   userdefault   

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************




好久没有更新cocos2d-x的东西了,是在太忙,木有时间啊= =。。




1.基本概念

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

bubuko.com,布布扣

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




2.获取

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

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

找到Userdefault.h

bool    getBoolForKey(const char* pKey);<span style="white-space:pre">		</span>
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);<span style="white-space:pre">		</span>// 二进制

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

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

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

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

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

<span style="font-family:Comic Sans MS;font-size:14px;">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;
}</span>

上面那个是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);
}

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

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********************************************

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

标签:cocos2d-x   简单数据存储   userdefault   

原文地址:http://blog.csdn.net/lttree/article/details/40897789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!