我的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, if the key doesn't exist, a default value will return.
	You can set the default value, or it is 0.
	*/
	int     getIntegerForKey(const char* pKey);
	int     getIntegerForKey(const char* pKey, int defaultValue);
	/**
	@brief Get float value by key, if the key doesn't exist, a default value will return.
	You can set the default value, or it is 0.0f.
	*/
	float    getFloatForKey(const char* pKey);
	float    getFloatForKey(const char* pKey, float defaultValue);
	/**
	@brief Get double value by key, if the key doesn't exist, a default value will return.
	You can set the default value, or it is 0.0.
	*/
	double  getDoubleForKey(const char* pKey);
	double  getDoubleForKey(const char* pKey, double defaultValue);
	/**
	@brief Get string value by key, if the key doesn't exist, a default value will return.
	You can set the default value, 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());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值