我的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());
- 我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault
- cocos2d-x学习之二:cocos2d-x之CCUserDefault使用
- iOS cocos2d-X之CCUserDefault的使用(二)
- Cocos2d-X 学习笔记 15 cocos2dx之CCUserDefault及其数据进行加密解密
- cocos2d-X之CCUserDefault
- 我的Cocos2d-x学习笔记(二十四)CCScrollView
- cocos2d-x 清除 CCUserDefault 数据
- cocos2d-x 数据存储 CCUserDefault
- 我的Cocos2d-x学习笔记(十三)CCLabelTTF、CCLabelAtlas、CCLabelBMFont
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- cocos2d-x学习笔记:记录存储1:CCUserDefault
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- Cocos2d-x笔记:清空CCUserDefault数据,获取时间(每日领取),子弹拖尾,字体
- Cocos2d-x教程(9)-简单的数据存储,CCUserDefault
- Cocos2d-x教程(9)-简单的数据存储,CCUserDefault
- cocos2d-x中保存用户游戏数据CCUserDefault (转载)
- cocos2d-x的初步学习二十三之模仿微信打飞机二
- 验证邮箱的正则表达式js代码详解
- 求两个有序整型数组的交集
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 【2·未知攻焉知防】 如何利用SQL注入,结合图片马渗透入侵服务器
- DSP/BIOS入门
- 我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault
- POJMatrix Power Series
- 【YouVersion】 The Bible 圣经App
- Linux服务器下LVS+Keepalived 高可用负载均衡集群架构实现
- 内容提供器(Content Provider)
- 详解软件无线电(SDR)架构
- 容斥原理
- 读AbstractQueuedSynchronizer类源码
- 【Android】多个Activity之间利用bundle传递数值