Cocos2dx 3.x Value, Vector and Map, cocos2dxvector

Source: Internet
Author: User

Cocos2dx 3.x Value, Vector and Map, cocos2dxvector
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));    }



Which version is based on Cocos2dx, 2x or 3x?

This is developed by an open-source community and is not developed by any company.
Go to its homepage and check the bottom line.
"Cocos2D-x open source developer community"

The sample code in this book is based on which version of Cocos2dx is written, is it 2x or 3x?

It must be 2.X.
After all, it wasn't long before 3.0 of the books came out. books were not so fast to produce...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.