Cocos2dx 3.0 过渡篇(十九)新鲜出炉的Value该怎么玩

来源:互联网 发布:廖雪峰python教程 爬虫 编辑:程序博客网 时间:2024/04/28 23:13
    在“巡视“cocos2dx 3.0rc0 新特性时,突然发现了一”诡异“身影:Value 。这货咋这么面熟呢?想了一会恍然大悟。value不就是”价值“的意思么?!不禁再次感慨自己英语水平又有见长。
    一开始使用CCArray时,想往里塞int型或者float型的变量都不行,只因为int等类型的身份”过低“,不是Object的家族成员。一心奔小康的我果断向贵族成员:CCInteger,CCFloat等靠拢。后来呢,也就是到了3.0beta后的版本,CCArray和CCDictionary一起殉情了,这直接导致CCInteger,CCFloat在Object的家族地位急剧降低。作为一家之主的Object虽不想看到自己的子孙就此没落,但因为年岁过大也无力改变现状。于是呢,999岁的Object趁着自己还有最后一点...精力...的情况下,赶紧娶了一个叫Ref的妙龄少女,在多晚的加班加点后成功诞生了一名叫Value的baby。时间如流水,转眼间Object已退居幕后,安心养老;Ref取代Object家主的地位母仪天下;CCInter等失宠后被发配边疆,再过一段时间应该会永远消失在人们的视线。而作为CCInteger等的替代者,Value开始走上岗位,散发他的光与热,最后它将穿越到神魔大陆,带领农民武装起义,并成功与齐天大圣结为兄弟,在克服了十二金钗的百般阻挠后成功武破虚空,江湖气短,儿女情长....当然了,这些都是后话了。请收起你手里愤怒的西瓜刀

下面到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 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());

输出如下图所示:



恩,就这样。


本文参考链接: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


4 2
原创粉丝点击