在Cocos2d-x存储数据使用的类是UserDefault类,以下分析下该类的使用

//.h
#include "base/CCPlatformMacros.h"
#include <string>
#include "base/CCData.h" NS_CC_BEGIN /**
* @addtogroup data_storage
* @{
*/ /**
* 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;
}; // end of data_storage group
/// @} NS_CC_END

使用时在自己定义类的.cpp文件里加入UserDefault.h,使用离子例如以下:

//.cpp
#include "base/CCUserDefault.h UserDefault::getInstance()->setStringForKey("key", "value");
auto value = UserDefault::getInstance()->getStringForKey("key","Default value");
log("key = %s",value.c_str());

执行结果例如以下图:控制台打印出 key = value;

Cocos2d-x3.1UserDefaule类具体解释的更多相关文章

  1. Java String类具体解释

    Java String类具体解释 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,非常多时候,我们对它既熟悉又陌生. 类结构: public fin ...

  2. Away3d 基础 1 ---对一个简单类的解释

    转自:http://www.cnblogs.com/nooon/archive/2009/05/16/1458334.html 原英文地址: http://www.flashmagazine.com/ ...

  3. Java-WebSocket 项目的研究(三) WebSocketClient 类 具体解释

    通过之前两篇文章 Java-WebSocket 项目的研究(一) Java-WebSocket类图描写叙述 Java-WebSocket 项目的研究(二) 小试身手:client连接server并发送 ...

  4. Explanation About Initilizing A DirextX3D Class 关于初始化Direct3D类的解释

    目录 DirectX11 Study Note Create a DirectX graphics interface factory.创建一个DirectX图形界面工厂 CreateDXGIFact ...

  5. Java 反射的用法 有关Class类的解释

    package com.imooc.test;public class ClassDemo1 { public static void main(String[] args) { Foo fool = ...

  6. java多线程Future和Callable类的解释与使用

    一,描写叙述 ​在多线程下编程的时候.大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取每一个线程中返回的数据然后再做统一处理,在这种需求下,Future与Callable的组合就派 ...

  7. [图像类名词解释][ RGB YUV HSV相关解释说明]

    一.概述 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.但被描述的颜色对象本身是客观的,不同颜色空间只是从不同的角度去衡量同一个对象.颜色空间按照基本机 ...

  8. Hibernate实体类注解解释

    Hibernate注解1.@Entity(name="EntityName")必须,name为可选,对应数据库中一的个表2.@Table(name="",cat ...

  9. cocos2d中个类之间的关系

    1.Director类: (1)单例类Director::getInstance()  ,获取导演类对象 (2)设置游戏配置(OpenGL),推动游戏发展 runWithSence.replaceSe ...

随机推荐

  1. 高仿拉手网底部菜单实现FragmentActivity+Fragment+RadioGroup

    先把欢迎页和引导页的代码上传了http://download.csdn.net/detail/u013134391/7183787不要积分的. 底部菜单条实如今4.0曾经都是用tabhost.如今基本 ...

  2. 创建一个jQuery UI的垂直进度条效果

    日期:2013-9-24  来源:GBin1.com 在线演示 缺省的jQuery UI只有水平的进度条效果,没有垂直的进度条效果,仅仅重新定义JQuery UI的CSS不能解决这个问题. 这里我们扩 ...

  3. [IDEA学习笔记][keymap]

    一个总站: http://www.youmeek.com/ 常用的快捷键keymap 提示: ctrl+N:快速打开一个类 Ctrl+P 方法参数提示显示 Ctrl+J 提示自定义模板 Ctrl+O ...

  4. Https协议简析及中间人攻击原理

    1.基础知识 1.1 对称加密算法 对称加密算法的特点是加密密钥和解密密钥是同一把密钥K,且加解密速度快,典型的对称加密算法有DES.AES等                              ...

  5. 使用Fiddler捕获Java程序中的HTTP请求

      默认Java程序是不支持Fiddler获取请求的,因此需要通过设置代理来实现.   Fiddler的端口是8888,若Fiddler没有运行,则会抛出异常.   HttpClient4.5示例: ...

  6. Ext Radio 取消选中

    今天,做项目的时候遇到了要吧Ext Radio单选按钮取消选中状态,由于没有在formpanel中写, 导致不能用reset()方法,试了各种方法,最后这样写管用. radio1.setValue(f ...

  7. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  8. QTestlib Manual翻译

    Trolltech公司提供的QTestlib框架,是一种针对基于QT编写的程序或库的单元测试工具.QTestLib提供了单元测试框架的基本功能,并提供了针对GUI测试的扩展功能. 目录: QtestL ...

  9. java程序的10个调试技巧

    参看下面链接:http://www.kuqin.com/java/20120906/330130.html

  10. PHP--变量部分知识点

    PHP全局变量 PHP全局变量作用域不同与C,在函数内部不可以使用全局变量,要在函数内部使用全局变量需要,global $var或者使用超全局变量数组$GLOBALS['var']. 静态变量 PHP ...