在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. Direct3D 纹理映射

    纹理映射是将2D的图片映射到一个3D物体上面,物体上漂亮图案被称为纹理贴图, 一个表面可以支持多张贴图等等,下面简单介绍下纹理贴图 纹理贴图UV: 贴图是一个个像素点组成,每一个像素点都由一个坐标最后 ...

  2. Unity 白猫操作小实例

    最近师兄找我说白猫的操作如何做,  0.0 结果白猫没有android的客户端玩不了,看了下视频介绍就简单做了下 效果图:   核心代码: using UnityEngine; using Syste ...

  3. python threading基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...

  4. python高级编程之超类02:super的缺陷

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #当使用多重继承层次结构时,再使用super的时候是非常危险的,主要 ...

  5. ARC 工作原理

    自动引用计数(Automatic Reference Counting),是一个编译期间工作的能够帮你管理内存的技术. ARC在编译期间为每个Objective-C指针变量添加合适的retain, r ...

  6. Qt使用异或进行加密解密

          在加密,解密中,异或运算应该时比较简单的一种.下面的代码,采用异或运算进行加密,解密: 点击(此处)折叠或打开 #include <QtCore/QCoreApplication&g ...

  7. 飘逸的python - hack输出流便于调试

    当项目有很多文件时,要找出控制台的输出是在哪里print出来的很麻烦,不过这事对于强大的python来说小菜一碟. 先上代码和效果,再说明. import sys,traceback class my ...

  8. UML学习-状态图

    1.状态图概述 状态图(Statechart Diagram)主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列,引起状态转移的事件(Event),以及因状态转移而伴随的动作( ...

  9. asp.net页面按Enter键IE不提交表单

    //当按下回车键时,让指定的按钮获取指定的文本框的事件                this.txtFNick.Attributes.Add("onkeydown", " ...

  10. C#控制条码打印机 纸张大小,间距,绘制内容(所有条码打印机通用)

    其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢 using System; using System.Drawing; using System. ...