cocos2d-x学习笔记16:记录存储1:CCUserDefault
2013-11-06 16:38
204 查看
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。http://4137613.blog.51cto.com/4127613/770754
一、简述
CCUserDefalt作为NSUserDefalt类的cocos2d-x实现版本,承担了cocos2d-x引擎的记录实现功能。
他的接口非常简单。
bool getBoolForKey (const char *pKey, bool defaultValue=false) //Get bool value by key, if the key doesn't exist, a default value will return. int getIntegerForKey (const char *pKey, int defaultValue=0) //Get integer value by key, if the key doesn't exist, a default value will return. float getFloatForKey (const char *pKey, float defaultValue=0.0f) //Get float value by key, if the key doesn't exist, a default value will return. double getDoubleForKey (const char *pKey, double defaultValue=0.0) //Get double value by key, if the key doesn't exist, a default value will return. std::string getStringForKey (const char *pKey, const std::string &defaultValue="") //Get string value by key, if the key doesn't exist, a default value will return. 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.
在helloworld中增加如下代码:
CCUserDefault *save=CCUserDefault::sharedUserDefault(); save->setBoolForKey("bool_value",true); save->setDoubleForKey("double_value",0.1); save->setFloatForKey("float_value",0.1f); save->setIntegerForKey("integer_value",1); save->setStringForKey("string_value","test");
然后写入存档就完成了。
读取也很简单,用对应的get函数即可。但是,我不建议你使用get函数的缺省返回值,尤其是在没有生成存档的时候。
二、CCUserDefalt的问题
1.没有记录和表的概念
你会发现,如果要设置多存档,必须自己操作,而且代码会变得复杂,容易出错。
对于简单的游戏可以使用CCUserDefalt,但是对于复杂游戏,可以考虑使用SQLite。
2.没有数据类型安全
比如,如果你错写把一个Integer按Bool读取,是没有错误提示的
3.没有存档数据完整性的校验
我们找到之前的存档记录,用CCUserDefault::getXMLFilePath()可以获得存档位置,打开它
可以看到存档是明文的xml,如果玩家篡改了数据,你无从知晓。这个可以自己增加一个校验,比如crc,哈希之类的。
三、存档和游戏初始化的建议流程
一个建议的流程是:
if(!档案不存在) { 使用缺省数据写入存档; } 读取存档并初始化数据;
这是我在开发时使用的,在没有存档时首先写入一个,然后再读取。这减小了编码量,保证主要流程清晰。
那么如何判断存档不存在呢?我之前想用标准c++的fstream函数,但是如果从CCUserDefalt中用getXMLFilePath获得存档路径的话。如果此时存档文件不存在,就会自动生成一个。所以接下来的判断存档是否存在代码就会失效了。
yanghuiliu的blog中提到了一个方法,我其实不建议使用这种缺省返回值的方式,但是cocos2dx就设计成这样了,所以可以使用这种方法。
CCUserDefault *save=CCUserDefault::sharedUserDefault(); if(save->getBoolForKey("isExisted")) { //相关操作 save->setBoolForKey("isExisted",true); }
参考文献:
cocos2d-x中保存用户游戏数据CCUserDefault:http://blog.csdn.net/yanghuiliu/article/details/6912612
本文出自 “老G的小屋” 博客,请务必保留此出处http://4137613.blog.51cto.com/4127613/770754
相关文章推荐
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- cocos2d-x学习笔记16:记录存储1:CCUserDefault
- cocos2d-x学习笔记16:记录存储1:CCUserDefault 推荐
- cocos2d-x学习笔记:记录存储1:CCUserDefault
- Cocos2d-X 学习笔记 16 使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码加密
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用 推荐
- cocos2d-x学习笔记19:记录存储3:使用摘要算法进行存档校验 推荐
- Cocos2d-x学习笔记(16)(常见22种特效)
- 【Cocos2d-X开发学习笔记】第30期:游戏中数据的存储(下)
- 【Cocos2d-X开发学习笔记】第29期:游戏中数据的存储(上)
- 【Cocos2d-X开发学习笔记】第30期:游戏中数据的存储(下)
- cocos2d-x 3.1.1 学习笔记[16] Particle 粒子效果
- cocos2d-x学习笔记--第一天记录
- Cocos2d-x学习笔记(16)(常见22种特效)
- 我的Cocos2d-x学习笔记(二十三)数据持久化之CCUserDefault
- 【学习笔记----数据结构16-图的存储结构】
- 【cocos2d-x】cocos2d-x学习笔记之二——相关网站记录