cocos2dx:数据存储-UserDefault
来源:互联网 发布:电脑版淘宝店招在哪里 编辑:程序博客网 时间:2024/05/20 03:47
游戏中一些比较简单的数据可以使用UserDefault来保存,它的存储方式为xml文件格式。
UserDefault定义如下所示:
/**
* UserDefault 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
*/
class CC_DLL UserDefault
{
public:
// get value methods
/**
@brief Get bool value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is false.
* @js NA
*/
bool getBoolForKey(const char* pKey);
/**
* @js NA
*/
bool getBoolForKey(const char* pKey, bool defaultValue);
/**
@brief Get integer value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.
* @js NA
*/
int getIntegerForKey(const char* pKey);
/**
* @js NA
*/
int getIntegerForKey(const char* pKey, int defaultValue);
/**
@brief Get float value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0f.
* @js NA
*/
float getFloatForKey(const char* pKey);
/**
* @js NA
*/
float getFloatForKey(const char* pKey, float defaultValue);
/**
@brief Get double value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is 0.0.
* @js NA
*/
double getDoubleForKey(const char* pKey);
/**
* @js NA
*/
double getDoubleForKey(const char* pKey, double defaultValue);
/**
@brief Get string value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is "".
* @js NA
*/
std::string getStringForKey(const char* pKey);
/**
* @js NA
*/
std::string getStringForKey(const char* pKey, const std::string & defaultValue);
/**
@brief Get binary data value by key, if the key doesn't exist, a default value will return.
You can set the default value, or it is null.
* @js NA
* @lua NA
*/
Data getDataForKey(const char* pKey);
/**
* @js NA
* @lua NA
*/
Data getDataForKey(const char* pKey, const Data& defaultValue);
// set value methods
/**
@brief Set bool value by key.
* @js NA
*/
void setBoolForKey(const char* pKey, bool value);
/**
@brief Set integer value by key.
* @js NA
*/
void setIntegerForKey(const char* pKey, int value);
/**
@brief Set float value by key.
* @js NA
*/
void setFloatForKey(const char* pKey, float value);
/**
@brief Set double value by key.
* @js NA
*/
void setDoubleForKey(const char* pKey, double value);
/**
@brief Set string value by key.
* @js NA
*/
void setStringForKey(const char* pKey, const std::string & value);
/**
@brief Set binary data value by key.
* @js NA
* @lua NA
*/
void setDataForKey(const char* pKey, const Data& value);
/**
@brief Save content to xml file
* @js NA
*/
void flush();
/** returns the singleton
* @js NA
* @lua NA
*/
static UserDefault* getInstance();
/**
* @js NA
*/
static void destroyInstance();
/** deprecated. Use getInstace() instead
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault();
/**
* @js NA
*/
CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault();
/**
* @js NA
*/
const static std::string& getXMLFilePath();
/**
* @js NA
*/
static bool isXMLFileExist();
private:
UserDefault();
~UserDefault();
static bool createXMLFile();
static void initXMLFilePath();
static UserDefault* _userDefault;
static std::string _filePath;
static bool _isFilePathInitialized;
};
使用如下:
UserDefault::getInstance()->setStringForKey("key","value"); //添加一个字符串数据到指定key下,UserDefault::getInstance()如果是第一次调用则会自动生成一个UserDefault.xml文件,该文件用以保存所有的UserDefault中的数据,UserDefault中的数据是以key-value的方式来存储的
UserDefault::getInstance()->getStringForKey("key","default value");//读取指定key的数据,如不存在该数据则返回指定的默认值"default value"
UserDefault::isXMLFileExist();//用以判断是否已经存在UserDefault.xml文件
如果嫌弃调用时代码过长,可以使用宏,如
#define SaveStringToXML UserDefault::getInstance()->setStringForKey
#define LoadStringFromXML UserDefault::getInstance()->getStringForKey
...
#define SaveBooleanToXML UserDefault::getInstance()->setBoolForKey
#define LoadBooleanFromXML UserDefault::getInstance()->getBoolForKey
有时需要判断是否首次运行/生成某些数据,可使用一个boolean值来进行记录,如if(LoadBooleanFromXML("first_time",true))
{
//进行所需的首次加载操作
SaveBooleanToXML ("first_time",false);
}
- cocos2dx:数据存储-UserDefault
- [cocos2dx]数据存储 UserDefault
- cocos2dx UserDefault存储数据问题
- cocos2dx数据存储 Userdefault数据存储实例
- cocos2dx一路走来之<API学习:数据存储UserDefault>
- cocos2dx之UserDefault: 数据存储及加密 (上)
- IOS基础:UserDefault存储小量数据
- Cocos2d-x数据篇01:UserDefault数据存储
- cocos2d-x 之 简单数据存储——Userdefault
- Cocos2d-x之简单数据存储 — Userdefault
- Cocos2d-x 3.x数据存储(UserDefault)
- cocos2d-x 之 简单数据存储——Userdefault
- cocos2d-x 之 简单数据存储——Userdefault
- cocos2dx中的游戏储存-UserDefault
- cocos2dx 3.6 关于场景间传值(UserDefault)、本地存储的基本方法(tinyxml2)
- UserDefault *defaults = UserDefault::getInstance();数据持久性
- [cocos2dx笔记016]cocos2dx 3.2 的UserDefault
- cocos2dx-数据存储
- hdu2097
- oc中将CGRect、CGSize、CGPoint等结构体转换为字符串
- 代码说明生成工具doxygen的使用
- 关于hmtl5的Views的一些知识
- 51单片机中断学习
- cocos2dx:数据存储-UserDefault
- [FreeMarker 2.3.20] Part I 关于模版设计的介绍 ~值和类型~ 类型
- Spring入门
- android ndk初体验和Android.mk文件报: no rule to make target XXX needed by.......
- Android模拟器与pc,模拟器与模拟之间用socket进行通信
- Jquery实现两个select框中元素操作
- NFC的安卓事儿(第三篇)
- php5 语言基础
- kermit,tftp,nfs的配置