Cocos2d-x 3.0数据结构——cocos2d::Value

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.0数据结构——cocos2d::Value前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d::Value是许多基本类型(int,float,double,bool,unsigned char,char*和std::string)还有std::vector<Value>,std::unordered_map<std::string,Value>std::unordered_map<int,Value>这些类的包装类型。

你可以将上面提及的基本类放进cocos2d::Value对象将它们转换成对应的类型,反之亦然。

内存管理

注意:cocos2d::Value不能像其它cocos2d类型一样使用retain/release和引用计数内存管理

示例

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

结果是:

cocos2d: val is null @H_404_139@ cocos2d: The description of the integer value: @H_404_139@ 65

cocos2d: val1.asByte() = A @H_404_139@ cocos2d: The description of the string value: @H_404_139@ string

cocos2d: The description of the Vector value: @H_404_139@ true

cocos2d: The description of the Map value: @H_404_139@ true

cocos2d: The description of the Value-type value: @H_404_139@ true

cocos2d: operator-> The description of val2: @H_404_139@ 65

cocos2d: operator-> The description of val4: @H_404_139@ 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相关文章