cocos2d-x学习(6)----CCUserDefault(XML数据)
2013-06-20 15:24
573 查看
1.CCUserDefault:
CCUserDefault acts as a tiny database.You can save and get base type values by it. For example, setBoolForKey("played", true) will add a bool value true into the database. Its key is "played". You can get the value of the key by getBoolForKey("played").
It supports the following base types: bool, int, float, double, string
CCUserDefault 是一个简单的数据库,它支持bool, int, float, double, string 5种数据的处理。 如果,你懂得Android的sharedpreferences,又你就完全理解这个类的作用!
2.关键的方法:
bool getBoolForKey (const char *pKey) Get bool value by key, if the key doesn't exist, a default value will return. bool getBoolForKey (const char *pKey, bool defaultValue) int getIntegerForKey (const char *pKey) Get integer value by key, if the key doesn't exist, a default value will return. int getIntegerForKey (const char *pKey, int defaultValue) float getFloatForKey (const char *pKey) Get float value by key, if the key doesn't exist, a default value will return. float getFloatForKey (const char *pKey, float defaultValue) double getDoubleForKey (const char *pKey) Get double value by key, if the key doesn't exist, a default value will return. double getDoubleForKey (const char *pKey, double defaultValue) std::string getStringForKey (const char *pKey) Get string value by key, if the key doesn't exist, a default value will return. std::string getStringForKey (const char *pKey, const std::string &defaultValue) void setBoolForKey (const char *pKey, bool value) Set bool value by key. void setIntegerForKey (const char *pKey, int value) Set integer value by key. void setFloatForKey (const char *pKey, float value) Set float value by key. void setDoubleForKey (const char *pKey, double value) Set double value by key. void setStringForKey (const char *pKey, const std::string &value) Set string value by key. void flush () Save content to xml file.
static CCUserDefault * sharedUserDefault () static void purgeSharedUserDefault () static const std::string & getXMLFilePath () static bool isXMLFileExist ()
3.代码样例:
// change the value CCUserDefault::sharedUserDefault()->setStringForKey("string", "value2"); CCUserDefault::sharedUserDefault()->setIntegerForKey("integer", 11); CCUserDefault::sharedUserDefault()->setFloatForKey("float", 2.5f); CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6); CCUserDefault::sharedUserDefault()->setBoolForKey("bool", false); CCUserDefault::sharedUserDefault()->flush(); // print value ret = CCUserDefault::sharedUserDefault()->getStringForKey("string"); CCLOG("string is %s", ret.c_str()); d = CCUserDefault::sharedUserDefault()->getDoubleForKey("double"); CCLOG("double is %f", d); i = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer"); CCLOG("integer is %d", i); f = CCUserDefault::sharedUserDefault()->getFloatForKey("float"); CCLOG("float is %f", f); b = CCUserDefault::sharedUserDefault()->getBoolForKey("bool");
参考:
1.http://www.cocos2d-x.org/reference/native-cpp/d0/d79/classcocos2d_1_1_c_c_user_default.html#ae11ba15de3458c8c7683c16c279fce162.http://blog.sina.com.cn/s/blog_61d2d3f50100x29x.html
相关文章推荐
- Cocos2d-X 学习笔记 15 cocos2dx之CCUserDefault及其数据进行加密解密
- Cocos2d-X 学习笔记 16 使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码加密
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据加密!
- Cocos2D-X笔记(5)CCUserDefault:保存数据
- cocos2d-x中保存用户数据使用CCUserDefault
- cocos2d-x 数据存储 CCUserDefault
- cocos2d-x 浅谈数据存储-CCUserDefault
- cocos2d-x学习之二:cocos2d-x之CCUserDefault使用
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- cocos2d-x 清除 CCUserDefault 数据
- 学习笔记之cocos2d-x2.1.1实现修改plist文件数据,用TinyXml解析XML
- Cocos2d-X游戏开发中的数据存储之CCUserDefault
- 学习笔记之cocos2d-x2.1.1实现修改plist文件数据,用TinyXml解析XML
- 设置、数值-【Cocos2D-X 】初窥门径(11)CCUserDefault:保存数据-by小雨
- cocos2d-x CCUserDefault 实现数据存储XML
- cocos2d-x 3.1.1 学习笔记[14] UserDefault 用户数据存储
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码!
- 【Cocos2D-X 游戏引擎】初窥门径(11)CCUserDefault:保存数据
- cocos2d-x CCUserDefault 无法写入数据
- Cocos2d-x笔记:清空CCUserDefault数据,获取时间(每日领取),子弹拖尾,字体