Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d::Value大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d::Value

  • Since: v3.0 beta
  • Language: C++

Defined in the head "CCValue.h" loated in "COCOS2DX_ROOT/cocos/base"


 
 
1
class Value;

cocos2d::Value is a wrapper class for many primitives(int,float,double,bool,unsigned char,char* and std::string) plus std::vector<Value>,std::unordered_map<std::string,Value> and std::unordered_map<int,Value>.

You can put all the primitives mentioned abovE into a cocos2d::Value object and convert it to the corresponding primitive. The opposite is vice verse.

Internally,cocos2d::Value uses a union variable to hold all kinds of primitives which saves a lot of memory space.

Before cocos2d-x v3.0 beta,there are CCBool,CCFloat,CCDouble,CCInteger primitive wrapper. these classes will be deprecated in the future.

Note:When you deal with primitives and container,please use cocos2d::Vector<T>,cocos2d::Map<K,V> and cocos2d::Value.

@H_342_84@memory Management

The memory of cocos2d::Value is handled automatically by it's own destructor. So please stick to the best practice of c++ memory management rules when handling the memory of cocos2d::Value.

The cocos2d::Value class contains the following data members:

 
 
1
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;

From the code snippets,_baseData,_strData and _type data members' memory are handled automatically by the compiler and their own destructors. The destructor of cocos2d::Value is responsible for deallocating all the resources of pointer member variables(_vectorData,_maPDAta and _intKeymaPDAta).

WARNING: cocos2d::Value doesn't use retain/release and refcount memory management like other cocos2d classes!

Basic Usage

The usage of cocos2d::Value is very sTraightforWARD.

Here is a simple usage example:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@L_450_41@
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Value val;   // call the default constructor
if (val.isNull()) {
    log("val is null");
}else{
    std::String str =val.getDescription();
    log("The description of val0:%s",str.c_str());
}
//----------------------------------------------------
Value val1(65);   // initialize with a Integer
//Value val1(3.4f);   // initialize with a float value
//Value val1(3.5);   // initialize with a double value
log("The description of the Integer value:%s",val1.getDescription().c_str());
log("val1.asByte() = %c",val1.asByte());
//----------------------------------------------------
std::String strV = "String";
Value val2(strV);   // initialize with String
log("The description of the String value:%s",val2.getDescription().c_str());
//----------------------------------------------------
auto sp0 = Sprite::create();
Vector<Object*>* vecV = new Vector<Object*>();
vecV->pushBACk(sp0);
Value val3(vecV);   // initialize with Vector
log("The description of the Vector value:%s",val3.getDescription().c_str());
delete vecV;
//----------------------------------------------------
Map<std::String,Object*>* mapV = new Map<std::String,Object*>();
mapV->insert(strV,sp0);
Value val4(mapV);   // initialize with Map
log("The description of the Map value:%s",val4.getDescription().c_str());
delete mapV;
//----------------------------------------------------
Value val6(&val4);   // initialize with Map
log("The description of the Value-type value:%s",val6.getDescription().c_str());
//----------------------------------------------------
val2 = val1;   // assigning between 2 Value-type
log("operator-> The description of val2:%s",val2.getDescription().c_str());
val2 = 4;   //assigning directly
log("operator-> The description of val4:%s",val2.getDescription().c_str());

output:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cocos2d: val is null
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

cocos2d: operator-> The description of val4:
4

Best Practice

  • Prefer cocos2d::Value and new template container(cocos2d::Vector<T> and cocos2d::Map<K,V>) over cocos2d::CCBool,cocos2d::CCFloat,cocos2d::CCDouble,cocos2d::CCString,cocos2d::CCInteger and old Objective-c style container(cocos2d::CCArray and cocos2d::CCDictionary).
  • When you want to deal with primitives aggregate,wrap the primitives with cocos2d::Value and combine them with the new template container cocos2d::Vector<T> and cocos2d::Map<K,V>.

大佬总结

以上是大佬教程为你收集整理的cocos2d::Value全部内容,希望文章能够帮你解决cocos2d::Value所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。