Cocos2d-x3.1UserDefaule类具体解释
在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类具体解释的更多相关文章
- Java String类具体解释
Java String类具体解释 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,非常多时候,我们对它既熟悉又陌生. 类结构: public fin ...
- Away3d 基础 1 ---对一个简单类的解释
转自:http://www.cnblogs.com/nooon/archive/2009/05/16/1458334.html 原英文地址: http://www.flashmagazine.com/ ...
- Java-WebSocket 项目的研究(三) WebSocketClient 类 具体解释
通过之前两篇文章 Java-WebSocket 项目的研究(一) Java-WebSocket类图描写叙述 Java-WebSocket 项目的研究(二) 小试身手:client连接server并发送 ...
- Explanation About Initilizing A DirextX3D Class 关于初始化Direct3D类的解释
目录 DirectX11 Study Note Create a DirectX graphics interface factory.创建一个DirectX图形界面工厂 CreateDXGIFact ...
- Java 反射的用法 有关Class类的解释
package com.imooc.test;public class ClassDemo1 { public static void main(String[] args) { Foo fool = ...
- java多线程Future和Callable类的解释与使用
一,描写叙述 在多线程下编程的时候.大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取每一个线程中返回的数据然后再做统一处理,在这种需求下,Future与Callable的组合就派 ...
- [图像类名词解释][ RGB YUV HSV相关解释说明]
一.概述 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.但被描述的颜色对象本身是客观的,不同颜色空间只是从不同的角度去衡量同一个对象.颜色空间按照基本机 ...
- Hibernate实体类注解解释
Hibernate注解1.@Entity(name="EntityName")必须,name为可选,对应数据库中一的个表2.@Table(name="",cat ...
- cocos2d中个类之间的关系
1.Director类: (1)单例类Director::getInstance() ,获取导演类对象 (2)设置游戏配置(OpenGL),推动游戏发展 runWithSence.replaceSe ...
随机推荐
- python高级编程之装饰器04
from __future__ import with_statement # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrat ...
- 字符(汉子)转换为ASCII
一般在jdk里面都会自包含一个官方提供的转换工具:native2ascii.exe 调用方法: 打开cmd界面,使用cd C:\Program Files\Java\jdk1.6.0_39\bin命 ...
- JAVA 代理模式(Proxy)
1.代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式一般涉 ...
- 我为什么放弃Go语言
有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?事实上我一直在认真思考这个问题. 开门见山地说,我当初放弃Go语言(golang),就是由于两个 ...
- 动态设置Head的Title、Descrption
HtmlMeta desc = new HtmlMeta(); desc.Name = "Description"; desc.Content = strTitle.ToStrin ...
- sqlserver2008 中使用MSXML2.ServerXMLHttp拼装soap调用webservice
要调用的接口方法:UP_ACC_inst_Info(string xml) 接口参数:xml格式的字符串 接口功能:传递人员编号.备注到接口进行更新,接口返回更新结果. 实例: declare @st ...
- OpenCV——Rect矩阵类
成员变量x.y.width.height,分别为左上角点的坐标和矩形的宽和高. 常用的成员函数有: Size()返回值为一个Size area()返回矩形的面积 contains(Point)用来判断 ...
- YUI的类型判断函数
1.首先定义一个关于类型的对象,及相关变量 类型判断对象 ar L = Y.Lang || (Y.Lang = {}), STRING_PROTO = String.prototype, TOSTRI ...
- 不用不知道 apply()与call()的强大
在看关于javascript继承的时候 好多地方都用到了apply()和call() 之前在简单编程的时候根本没有遇到过 查阅资料后才发现这两个方法是如此的好用. 下面从几方面来看一下这两个方法: 1 ...
- CSS布局部分知识总结
一 切图 通过一个周的切图练习,我发现只有亲自动手后才能通过问题不断加深技术,要熟练掌握HTML页面编写时候时PHOTOSHOP操作. 以下一些知识点在网络布局时也是很重要的. 1.ul标签在谷歌.火 ...