Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一、CCUserDefault简介

CCUserDefault是Cocos2d-x提供的持久化方案,其作用是存储所有游戏通用的用户配置信息。

CCUserDefault可以看做一个永久存储的字典,本质上是一个XML文件,将每个键及其对应的值以节点的形式存储到外存中。

由于每次设置和读取都会遍历整个XML树,效率不高,值类型也有限,适合小规模使用。

系统会在默认路径cocos2d - x - 2.2.6\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDefault.xml 的文件。所有的key 皆为char *型,value类型为bool int float double std::string.

二、CCUserDefault的使用

CCUserDefault类中部分代码如下:

class CC_DLL CCUserDefault
{
public:
	/**
	@brief Get bool value by key,if the key doesn't exist,a default value will return.
	You can set the default value,or it is false.
	*/
	bool    getBoolForKey(const char* pKey);
	bool    getBoolForKey(const char* pKey,bool DefaultValuE);
	/**
	@brief Get Integer value by key,or it is 0.
	*/
	int     getIntegerForKey(const char* pKey);
	int     getIntegerForKey(const char* pKey,int DefaultValuE);
	/**
	@brief Get float value by key,or it is 0.0f.
	*/
	float    getFloatForKey(const char* pKey);
	float    getFloatForKey(const char* pKey,float DefaultValuE);
	/**
	@brief Get double value by key,or it is 0.0.
	*/
	double  getDoubleForKey(const char* pKey);
	double  getDoubleForKey(const char* pKey,double DefaultValuE);
	/**
	@brief Get String value by key,or it is "".
	*/
	std::string getStringForKey(const char* pKey);
	std::string getStringForKey(const char* pKey,const std::string & DefaultValuE);

	// set value methods

	/**
	@brief Set bool value by key.
	*/
	void    setBoolForKey(const char* pKey,bool value);
	/**
	@brief Set Integer value by key.
	*/
	void    setIntegerForKey(const char* pKey,int value);
	/**
	@brief Set float value by key.
	*/
	void    setFloatForKey(const char* pKey,float value);
	/**
	@brief Set double value by key.
	*/
	void    setDoubleForKey(const char* pKey,double value);
	/**
	@brief Set String value by key.
	*/
	void    setStringForKey(const char* pKey,const std::string & value);
	/**
	@brief Save content to xml file
	*/
	void    flush();

	static CCUserDefault* sharedUserDefault();
	static void purgeSharedUserDefault();
	const static std::string& getXMLFilePath();
	static bool isXMLFileExist();

private:
	CCUserDefault();
	static bool createXMLFile();
	static void initXMLFilePath();

	static CCUserDefault* m_spUserDefault;
	static std::string m_sFilePath;
	static bool m_sbIsFilePathInitialized;
};

以上代码中包含注释,很容易看懂。

sharedUserDefault():获取CCUserDefault单例对象。

purgeSharedUserDefault():销毁CCUserDefault单例对象。

flush():通过此方法刷新,写入文件。

(一)BoolForKey

void    setBoolForKey(const char* pKey,bool value);
bool    getBoolForKey(const char* pKey);
bool    getBoolForKey(const char* pKey,bool DefaultValuE);

setBoolForKey:第一个参数为键,第二个参数为对应的值,布尔型存档。

getBoolForKey:根据传入的键值参数返回相应的布尔值,第二个参数为可选的默认值,如果该键值的值不存在,将返回默认值。

例:

	CCUserDefault::sharedUserDefault()->setBoolForKey("Gong",truE);
	CCUserDefault::sharedUserDefault()->flush();
	bool boolValue = CCUserDefault::sharedUserDefault()->getBoolForKey("Gong",falsE); 
	CCLog("%d",boolvalue);

(二)IntegerForKey

void    setIntegerForKey(const char* pKey,int value);
int     getIntegerForKey(const char* pKey);
int     getIntegerForKey(const char* pKey,int DefaultValuE);

setIntegerForKey:第一个参数为键,第二个参数为对应的值,整型存档

getIntegerForKey:根据传入的键值参数返回相应的整型值,第二个参数为可选的默认值,如果该键值的值不存在,将返回默认值。

例:

	CCUserDefault::sharedUserDefault()->setIntegerForKey("INT",5);
	CCUserDefault::sharedUserDefault()->flush();
	int intValue = CCUserDefault::sharedUserDefault()->getIntegerForKey("INT",6);
	CCLog("%d",intvalue);
(三)FloatForKey
void    setFloatForKey(const char* pKey,float value);
float    getFloatForKey(const char* pKey);
float    getFloatForKey(const char* pKey,float DefaultValuE);

setFloatForKey:第一个参数为键,第二个参数为对应的值,浮点型存档。

getFloatForKey:根据传入的键值参数返回相应的浮点值,第二个参数为可选的默认值,如果该键值的值不存在,将返回默认值。

例:

	CCUserDefault::sharedUserDefault()->setFloatForKey("FLOAT",3.12);
	CCUserDefault::sharedUserDefault()->flush();
	float floatValue = CCUserDefault::sharedUserDefault()->getFloatForKey("FLOAT",4.0);
	CCLog("%f",floatvalue);
(四)DoubleForKey
void    setDoubleForKey(const char* pKey,double value);
double  getDoubleForKey(const char* pKey);
double  getDoubleForKey(const char* pKey,double DefaultValuE);
setDoubleForKey:第一个参数为键,第二个参数为对应的值,双精度浮点型存档。

getDoubleForKey:根据传入的键值参数返回相应的双精度浮点型值,第二个参数为可选的默认值,如果该键值的值不存在,将返回默认值。

例:

	CCUserDefault::sharedUserDefault()->setDoubleForKey("DOUBLE",5.123);
	CCUserDefault::sharedUserDefault()->flush();
	double doubleValue = CCUserDefault::sharedUserDefault()->getDoubleForKey("DOUBLE",6.123);
	CCLog("%lf",doublevalue);
(五)StringForKey
void    setStringForKey(const char* pKey,const std::string & value);
std::string getStringForKey(const char* pKey);
std::string getStringForKey(const char* pKey,const std::string & DefaultValuE);
setStringForKey:第一个参数为键,第二个参数为对应的值,字符串型存档。
getStringForKey:根据传入的键值参数返回相应的字符串值,第二个参数为可选的默认值,如果该键值的值不存在,将返回默认值。

例:

	CCUserDefault::sharedUserDefault()->setStringForKey("StriNG","JianYiLiGong");
	CCUserDefault::sharedUserDefault()->flush();
	std::string str = CCUserDefault::sharedUserDefault()->getStringForKey("StriNG","GONG");
	CCLog("%s",str.c_str());

大佬总结

以上是大佬教程为你收集整理的我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault全部内容,希望文章能够帮你解决我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。