我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault

来源:互联网 发布:高仿运动服 淘宝 编辑:程序博客网 时间:2024/06/05 18:06

一、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