今天白白跟大家分享一下cocos2dx中游戏的存储及需要注意的事项

cocos2dx中自带了存储类:CCUserDefault ,倘若需要存储的数据量教大的话,建议使用数据库来存储

现在先给大家看一下CCUserDefault的API

Public Member Functions

    ~CCUserDefault ()
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.
void flush ()
Save content to xml file.
Static Public Member Functions static CCUserDefault * sharedUserDefault ()
static void purgeSharedUserDefault ()
static const std::string & getXMLFilePath ()

大家可以清楚的看到CCUserDefault这个类,存储是使用的是Key -Value,利用key来索引Value的值

现在我们举一个例子:


//存储并获取数据
CCUserDefault::sharedUserDefault()->setStringForKey("name", "baibai");
CCUserDefault::sharedUserDefault()->flush();//写了东西要提交
std::string name = CCUserDefault::sharedUserDefault()->getStringForKey("name");
CCLOG("name: %s ", name.c_str());

好了,现在我们就能打印出name: baibai

注意事项:

1、写好了数据一定要记得提交,CCUserDefault会把数据存储在UserDefault.xml中,这个文件就在cocos2d-x-2.2的Debug.win32目录下。可以打开这个文件查看储存的数据。

2、key有遵循命名规则,千万不能想当然的给他随意命名,白白之前就在这上面吃过亏,希望大家谨记。

白白之前给key命名了score[i]这是不对的。

我们再来写一个例子

	//保存
for (int i = 0; i < 5; ++i)
{
CCString* setScore = CCString::createWithFormat("a_%d",i);
CCUserDefault::sharedUserDefault()->setIntegerForKey(setScore->getCString(), a[i]);
}
CCUserDefault::sharedUserDefault()->flush();//提交
//获取
for (int i = 0; i < 5; ++i)
{
CCString* getScore = CCString::createWithFormat("a_%d",i);
int score[i] = CCUserDefault::sharedUserDefault()->getIntegerForKey(getScore->getCString());
CCLOG("score[%d]: %d", i, score[i]);
}

ok,现在这些数据就能做为排行榜使用了

最新文章

  1. 探讨Android中的内置浏览器和Chrome
  2. entityframework使用oracle的几个小问题
  3. Linux 图形化操作
  4. vsftp 一键安装包
  5. Clearing Floats清除浮动--clearfix的不同方法的使用概述
  6. zabbix3.0 安装方法,一键实现短信、电话、微信、APP 告警
  7. 【Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸】
  8. iOS8推送消息的快速回复处理
  9. 重磅︱R+NLP:text2vec包——New 文本分析生态系统 No.1(一,简介)
  10. 什么时候Python的List,Tuple最后一个Item后面要加上一个逗号
  11. pytorch打印模型结构图
  12. shell中关于file的判断(转载)
  13. spring boot 2 统一异常处理
  14. Python_装饰器习题_31
  15. NOIP2015跳石头
  16. 2.oracle之用户管理sql
  17. nginx路径设置(web)
  18. 1035 Password (20 分)
  19. OpenCV3.4.1+vs2017安装及配置
  20. 基于json的jquery地区联动探索

热门文章

  1. [ 转载 ] Mysql 远程连接+开放80和3306端口 常用配置
  2. TS Stream 详解
  3. sublime3176注册码破解汉化及常用插件
  4. BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
  5. with在模板中的应用
  6. [IOI2007]Miners
  7. TF-timeline的使用经验记录
  8. 【JavaScript代码实现四】获取和设置 cookie
  9. MessasgePack:一个小巧高效的序列化方式
  10. [转载] C-MEX程序编写