1. Value
Cocos2d: value is an external type that contains many native types (INT, float, double, bool, unsigned char, char *, and STD: string ).
Classes with STD: vector <value>, STD: unordered_map <STD: String, value> and STD: unordered_map <int, value>.
You can put all the native types mentioned above into the cocos2d: value object, convert them to the corresponding native type, and vice versa.
Value val; // call the default constructor 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); // use an int to initialize // value val1 (3.4f ); // use a float to initialize // value val1 (3.5); // use a double to initialize log ("the description of the integer value: % s", val1.getdescription (). c_str (); log ("val1.asbyte () = % C", val1.asbyte (); // Using STD: String strv = "string"; value val2 (strv ); // use string to initialize log ("the description of the string value: % s", val2.getdescription (). c_str (); // ---------------------------------------------------- auto sp0 = sprite: Create (); vector <object *> * vecv = new vector <object *> (); vecv-> pushback (sp0); Value val3 (vecv); // use a vector to initialize log ("the description of the Vector Value: % s", val3.getdescription (). c_str (); Delete vecv; // -------------------------------------------------- Map <STD: String, object *> * mapv = New Map <STD: String, object *> (); mapv-> insert (strv, sp0); Value val4 (mapv); // use map to initialize log ("the description of the MAP value: % s", val4.getdescription (). c_str (); Delete mapv; // -------------------------------------------------- value val6 (& val4); // use map to initialize log ("the description of the value-type value: % s ", val6.getdescription (). c_str (); // invalid val2 = val1; // values log ("operator-> the description of val2: % s", val2.getdescription () between two different types (). c_str (); val2 = 4; // values log ("operator-> the description of val4: % s", val2.getdescription (). c_str (); output: cocos2d: Val is nullcocos2d: The description of the integer value: 65cocos2d: val1.asbyte () = ACO: 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
Function and usage of value: When a value is created, a value is input to the constructor, and the value automatically determines its type based on the value. When obtaining the value, you can call the as ** function to obtain the value based on its type.
Integer, float, and String Conversion
Convert an integer to a string: STD: String STR = "no" + value (1). asstring ();
String to integer: log ("% d", value ("1234"). asint ())
Float-type conversion string: log ("% s", value (123.5f). asstring (). c_str ())
String to float: log ("% F", value ("14.45"). asfloat ())
2. Vector
Vector is a encapsulated container that can dynamically increase sequential access.
Function Description:
Size (): vector size
At (INDEX): returns the object whose vector subscript is index.
Pushback (object): Add an object at the end of the vector.
Eraseobject (object): removes an object from a vector.
Erase (INDEX): removes the object whose subscript is index from the vector.
Clear (): clears the vector.
How to traverse Vector
For (Auto OBJ: vector ){
...
}
3. Map
Map is an associated container that stores key-value pairs. It can quickly retrieve corresponding values through their keys.
Main functions:
Insert (Key, value): insert an object to the map.
At (key): returns the object with the key keyword in the map.
How to traverse map?
mapKeyVec = map1.keys(); for(auto key : mapKeyVec) { auto spTag = map1.at(key)->getTag(); log("The Sprite tag = %d, MAP key = %s",spTag,key.c_str()); log("Element with key %s is located in bucket %zd",key.c_str(),map1.bucket(key)); }
Knowledge about cocos2dx 3.x value, vector, and map