[Cocos2d-x v3.x官方文档]cocos2d::Value
来源:互联网 发布:密室逃脱成本计算软件 编辑:程序博客网 时间:2024/05/17 07:25
- v3.0加入
定义在”COCOS2DX_ROOT/cocos/base”的头文件”CCValue.h”中
class Value;
cocos2d::Value是许多基本类型(int,float,double,bool,unsigned char,char*和std::string)还有std::vector, std::unordered_map和std::unordered_map这些类的包装类型。
你可以将上面提及的基本类放进cocos2d::Value对象将它们转换成对应的类型,反之亦然。
cocos2d::Value底层用一个统一的变量来保存任意基本类型值,它将更加节省内存。在cocos2d-x v3.0beta之前使用的是CCBool, CCFloat, CCDouble和CCInteger这样基本类型包装类,不过它们将被废弃掉。
注意:当处理基本类型和和容器时,请使用cocos2d::Vector, cocos2d::Map和cocos2d::Value。
内存管理
cocos2d::Value的内存由它的析构函数来释放,所以使用cocos2d::Value时请尽量用推荐的最佳做法。
cocos2d::Value包含下面的数据成员:
union{ unsigned char byteVal; int intVal; float floatVal; double doubleVal; bool boolVal;}_baseData;</p>std::string _strData;ValueVector* _vectorData;ValueMap* _mapData;ValueMapIntKey* _intKeyMapData;Type _type;
代码段中,_baseData, _strData和_type是由编译器和它们的析构函数负责释放内存的,而cocos2d::Value的析构函数则负责释放指针成员(_vectorData, _mapData和intKeyMapData)。
注意:cocos2d::Value不能像其它cocos2d类型一样使用retain/release和refcount内存管理
基本用法
ocos2d::Value的用法非常简单直接。下面就是使用的例子:
Value val; // call the default constructorif (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 valuelog("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 stringlog("The description of the string value:%s",val2.getDescription().c_str());//----------------------------------------------------auto sp0 = Sprite::create();Vector<Ref*>* vecV = new Vector<Ref*>();vecV->pushBack(sp0);Value val3(vecV); // initialize with Vectorlog("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 Maplog("The description of the Map value:%s",val4.getDescription().c_str());delete mapV;//----------------------------------------------------Value val6(&val4); // initialize with Maplog("The description of the Value-type value:%s",val6.getDescription().c_str());//----------------------------------------------------val2 = val1; // assigning between 2 Value-typelog("operator-> The description of val2:%s",val2.getDescription().c_str());val2 = 4; //assigning directlylog("operator-> The description of val4:%s",val2.getDescription().c_str());
结果是:
cocos2d: val is nullcocos2d: The description of the integer value:65cocos2d: val1.asByte() = Acocos2d: The description of the string value:stringcocos2d: The description of the Vector value:truecocos2d: The description of the Map value:truecocos2d: The description of the Value-type value:truecocos2d: operator-> The description of val2:65cocos2d: operator-> The description of val4:4
推荐用法
- 考虑使用cocos2d::Value和新的模版容器(cocos2d::Vector和cocos2d::Map)优于使用cocos2d::CCBool, cocos2d::CCFloat,cocos2d::CCDouble,cocos2d::CCString,cocos2d::CCInteger和旧的objective-c风格的容器(cocos2d::CCArray和cocos2d::CCDictionary)。
- 当要使用基本类型的聚合时,将基本类型包装成cocos2d::Value,然后将它们和模版容器cocos2d::Vector和cocos2d::Map联合使用。
- [Cocos2d-x v3.x官方文档]cocos2d::Value
- Cocos2d-x官方中文文档 v3.x
- [Cocos2d-x v3.x官方文档]cocos2d::Vector
- [Cocos2d-x v3.x官方文档]cocos2d::Map 分享0
- [Cocos2d-x v3.x]cocos2d::Value
- [Cocos2d-x v3.x官方文档]场景转换(Transitions)
- Cocos2d-x v3.x官方文档]动作 (Action)
- [Cocos2d-x v3.x官方文档]调度器(scheduler)
- [Cocos2d-x v3.x官方文档]事件分发机制
- Cocos2d-x v3.x官方文档: 事件分发机制
- Cocos2d-x v3.x官方文档]菜单(Menu) 分享0
- Cocos2d-x官方中文文档 v3.x HttpClient
- [Cocos2d-x v3.x官方文档]菜单(Menu)
- Cocos2d-x v3.x官方文档]场景转换(Transitions)
- [Cocos2d-x v3.x官方文档]纹理缓存
- 【Cocos2d-x v3.x官方文档】序列帧动画
- 【Cocos2d-x v3.x官方文档】序列帧动画
- [Cocos2d-x v3.x官方文档]纹理缓存
- 10.6 匿名内部类
- java取当前周期、月初至月末、季度初至季度末日期。
- 非常简单的Socket聊天程序
- apk编译流程
- LAMP建立web服务器
- [Cocos2d-x v3.x官方文档]cocos2d::Value
- 通过函数调用,获取字符串的方法
- 回调函数
- [ICME 2014, paperId 293]AN APPROACH FOR FAST AND PARALLEL VIDEO PROCESSING ON APACHE HADOOP CLUSTERS
- 海贼女帝(程序员手绘)
- 10.6.1 再访工厂方法
- Nginx负载均衡的研究
- 使用Jquery easyui
- IIS MVC 发布错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容