下面到CCValue.h 中看看Value到底是怎么一回事。我们就挑一些典型的API来看看吧
explicit Value(int v);
xplicit Value(float v);//直接给一个Value副职,如Value(10.0);
Value& operator= (int v);
Value& operator= (float v);//创建一个变量再赋值,如:Value val(10.0);
int asInt() const;//将Value转换成整型
float asFloat() const;//浮点型
//判断Value 是否为空
inline bool isNull() const { return _type == Type::NONE; }
std::string getDescription();//得到一个Value的描述,返回类型为string型
inline Type getType() const { return _type; };//得到Value的类型
//Value总共有以下几种类型:
enum class Type
{
NONE,
BYTE,
INTEGER,
FLOAT,
DOUBLE,
BOOLEAN,
STRING,
VECTOR,
MAP,
INT_KEY_MAP
};下面列举一些简单的应用:
//----------------------------------------------------
Value val; // 调用默认的构造函数,初始化为空
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); // 初始化为整型
//Value val1(3.4f); // 初始化为浮点型
log("The description of the integer value val1:%s",val1.getDescription().c_str());//输出val1的描述(也就是它的内容)
log("val1.asByte() = %c",val1.asByte());//将int型转换成byte型(A)
//----------------------------------------------------
std::string strV = "10";
Value val2(strV); // 初始化为string型
log("The description of the string value val2:%s",val2.getDescription().c_str());
int val_int = val2.asInt();//将string型转换成int型(就是这么好用)
log("turn string to int ,the val_int value:%d",val_int);
//---------下面在列举一些更深一点的应用----------------------------
auto sp0 = Sprite::create();
Vector<Ref*>* vecV = new Vector<Ref*>();
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, Ref*>* mapV = new Map<std::string, Ref*>();
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());
恩,就这样。
本文参考链接:https://github.com/cocos2d/cocos-docs/blob/master/manual/framework/native/data-structure/v3/value/en.md
尊重原创,转发请注明来源:http://blog.csdn.net/start530/article/details/21651751
Cocos2dx 3.0 过渡篇(十九)CCInteger等的继承者Value该怎么玩,布布扣,bubuko.com
Cocos2dx 3.0 过渡篇(十九)CCInteger等的继承者Value该怎么玩
原文:http://blog.csdn.net/start530/article/details/21651751