0

I am trying to update my code from cocos2d-X version 2.X to version 3.X.While updating facing few problems with deprecated methods.I found that ValueVector is replacement for CCArray.Below is my code.

             CCArray *array = CCArray::create();

             CCArray *arra1 = CCArray::create();
             std::string winLine4[] = {"a", "b", "c", "d"};
             for (int i = 0; i < 4; i++) {
                  arra1->addObject(CCStringMake(winLine4[i]));
             }
             array->->addObject(arra1);

             CCArray *arra2 = CCArray::create();
             std::string winLine4[] = {"aa", "bb", "cc", "dd"};
             for (int i = 0; i < 4; i++) {
                  arra2->addObject(CCStringMake(winLine4[i]));
             }
             array->->addObject(arra2);

I tried with value map and value vectors but there are many type casting problems occuring.Please help me

1 Answers1

1

Value/ValueVector/ValueMap are fine for JSON or PLIST data, so if you're only using numbers, strings, and other vectors or dictionaries. Using your example it's similar, but you wrap types in Value() that are later read/parsed out with asString(), asInt(), asValueMap(), etc.

ValueVector arr;
ValueVector arr1;
ValueVector arr2;

std::string winLine4[] = {"a", "b", "c", "d"};
for (int i = 0; i < 4; i++) {
    arr1.push_back(Value(winLine4[i]));
}
arr.push_back(Value(arr1));

std::string winLine4[] = {"aa", "bb", "cc", "dd"};
for (int i = 0; i < 4; i++) {
    arr2.push_back(Value(winLine4[i]));
}
arr.push_back(Value(arr2));

To later access the values in the container.

Value v = arr.at(3);
string line = v.asString();
// get each Value inside vector
for(auto v : arr) {
   auto line = v.asString();
}

work with valueMap (ie: dictionary)

// create 
ValueMap dict;
dict["an_int"] = Value(3);
dict["a_string"] = Value("test string");
dict["a_float_in_string"] = Value("3.4");

// access 
int v1 = dict["an_int"].asInt();
std::string v2 = dict["a_string"].asString();
int v3 = dict["a_float_in_string"].asInt();
// you may want to check if key exists
Value v4 = dict["not_a_key"];
// v4.isNull() will be true
Steve T
  • 7,729
  • 6
  • 45
  • 65