首页 > 其他 > 详细

cocos2dx中的用户数据的管理

时间:2014-11-14 14:01:38      阅读:277      评论:0      收藏:0      [点我收藏+]

提供了专门的类: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中的用户数据的管理

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!