Cocos2d-x3.0模版容器详解之三:cocos2d::Value
发布时间:2020-12-14 21:09:57 所属栏目:百科 来源:网络整理
导读:1.概述 版本: v3.0 beta 语言: C++ 定义在 “COCOS2DX_ROOT/cocos/base” 路径下的 " CCValue.h " 的头文件中。 ? 1 class Value; cocos2d::Valie 是一个包含了很多原生类型(int,float,double,bool,unsigned char,char* 和 std::string)外加 std::vector
1.概述
定义在 “COCOS2DX_ROOT/cocos/base” 路径下的 " CCValue.h " 的头文件中。
你可以把所有上面的提及的原生类型放入 cocos2d::Value 对象中,然后将它们转化为对应的原生类型,反之亦然。 在内部,cocos2d::Value 使用了一个联合变量来保存各种原生类型,这样可以节省很多的内存空间。 在 Cocos2d-x v3.0 beta 之前,存在着一些原生类型的封装类,如 CCBool,CCFloat,CCDouble,CCinteger,这些将会被弃用。 注意: 当你在处理原生类型和容器的时候,请使用 cocos2d::Vector<T>,cocos2d::Map<K,V> 和 cocos2d::Value。 2.内存管理 cocos2d::Value 的内存是由它自己的析构函数自动处理的。所以当处理 cocos2d::Value 的内存时请坚持以 c++ 内存管理规则进行最佳实践。 cocos2d::Value 类包含了以下的数据成员:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
union
{
unsigned
char
byteVal;
int
intVal;
float
floatVal;
double
doubleVal;
bool
boolVal;
}_baseData;
std::string _strData;
ValueVector* _vectorData;
ValueMap* _mapData;
ValueMapIntKey* _intKeyMapData;
Type _type;
|
警告: cocos2d::Value 不再像其他的 cocos2d 类一样使用 retain/release和引用计数内存管理。
3.基本用法
cocos2d::Value 的用法是非常简单的。
这里提供一个简单的示例:
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Value val;
// 调用默认构造函数
if
(val.isNull()) {
log
(
"val is null"
);
}
else
{
std::string str =val.getDescription();
"The description of val0:%s"
,str.c_str());
}
//----------------------------------------------------
Value val1(65);
// 用一个 int 初始化
//Value val1(3.4f); // 用一个 float 初始化
//Value val1(3.5); // 用一个 double 初始化
"The description of the integer value:%s"
"val1.asByte() = %c"
//----------------------------------------------------
std::string strV =
"string"
;
Value val2(strV);
// 用 string 初始化
"The description of the string value:%s"
//----------------------------------------------------
auto
sp0 = Sprite::create();
Vector<Object*>* vecV =
new
Vector<Object*>();
vecV->pushBack(sp0);
Value val3(vecV);
// 用 Vector 初始化
"The description of the Vector value:%s"
delete
vecV;
//----------------------------------------------------
Map<std::string,Object*>* mapV =
Map<std::string,Object*>();
mapV->insert(strV,sp0);
Value val4(mapV);
// 用 Map 初始化
"The description of the Map value:%s"
mapV;
//----------------------------------------------------
Value val6(&val4);
// 用 Map 初始化
"The description of the Value-type value:%s"
//----------------------------------------------------
val2 = val1;
// 在两个不同指类型间赋值
"operator-> The description of val2:%s"
val2 = 4;
// 直接赋值
"operator-> The description of val4:%s"
输出:
cocos2d: The description of the integer value:
65
cocos2d: val1.asByte() = A
cocos2d: The description of the string value:
string
cocos2d: The description of the Vector value:
true
cocos2d: The description of the Map value:
true
cocos2d: The description of the Value-type value:
true
cocos2d:
operator
-> The description of val2:
65
-> The description of val4:
4
4.最佳实践
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!