码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx中的用户数据的管理

时间:2014-11-14 14:04:24      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ar   os   使用   sp   for   文件   数据   on   log   

提供了专门的类:CCUserDefault用来管理,且提供了单例方法:sharedUserDefault()

1.会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDefault.xml 的文件,xml文件中存储的是用户的数据,以键值对的形式存储

2.支持的数据类型:

所有的key 皆为char *型,

value:类型为bool int float double std::string.

3.使用方法:

设置:set方法,获取,get方法

void setBoolForKey(const char* pKey, bool value);
void setIntegerForKey(const char* pKey, int value);
void setFloatForKey(const char* pKey, float value);
void setDoubleForKey(const char* pKey, double value);
void setStringForKey(const char* pKey, const std::string & value);

bool getBoolForKey(const char* pKey);
bool getBoolForKey(const char* pKey, bool defaultValue); //第二个参数为带默认值的,如果要获得的key不存在,则返回默认值
int getIntegerForKey(const char* pKey);
int getIntegerForKey(const char* pKey, int defaultValue);
float getFloatForKey(const char* pKey);
float getFloatForKey(const char* pKey, float defaultValue);
double getDoubleForKey(const char* pKey);
double getDoubleForKey(const char* pKey, double defaultValue);
std::string getStringForKey(const char* pKey);

std::string getStringForKey(const char* pKey, const std::string &defaultvalue);

4.写入磁盘:

CCUserDefault::sharedUserDefault()->flush();//刷新到磁盘,上面设置的key-value在内存,只有刷新才会到硬盘中去

 5.与xml文件相关的操作:

获取xml文件的路径:

CCString path=CCUserDefault::sharedUserDefault()->getXMLFilePath();//获取xml文件的路径

 CCLog("xmlfile path is %s", path.getCString());

判断xml文件是否存在

CCLog("xmlfile exist is %d", CCUserDefault::sharedUserDefault()->isXMLFileExist());
 

cocos2dx中的用户数据的管理

标签:ar   os   使用   sp   for   文件   数据   on   log   

原文地址:http://www.cnblogs.com/ttss/p/4096957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!