10, COCOS2DX 3.0 game development to find small three container: Vector, Map, Value

Source: Internet
Author: User
Tags addchild

the labor outcome of the re-developers. When reproduced, please be sure to indicate the source : http://blog.csdn.net/haomengzhu/article/details/27705613


Container

3.0 version number before the Cocos2d-x engine provides us with Ccarray, ccdictionary and other objective-c-style containers;

One important reason to use cocos2d-x containers is the memory management of Cocos2d-x.


in general, the objects that are stored in the container should be guaranteed before being removed effective.

It is noteworthy, however, that the data structure vector has been added to the v3.0 beta version number.

Defined in the "CCVector.h" header file for "Cocos/base".

Template<class T>class Cc_dll Vector;


Cocos2d::vector<t> is a well-packaged container that can dynamically grow sequential access.

Before Cocos2d-x v3.0 Beta, another sequential visit to the container Cocos2d::ccarray was used, except that it would be discarded.

will use cocos2d::vector<t> to replace Cocos2d::ccarray.

Therefore, the use of cocos2d::vector<t> should be given priority in the use.


Use of vectors:

Create a container

Vector<sprite*> Sp_vec;

Add the created sprite into the container

Auto Sp1=sprite::create ("Closenormal.png");  Sp1->setposition (Point (50,50)); This->addchild (SP1);  Sp_vec.pushback (SP1);  

get the size of the container
int count=sp_vec.size ();

get the Genie in the container and let these elements do a uniform action
for (auto& E:sp_vec)   {      e->runaction (moveto::create (0.2f,point (200,200)));//This is a new feature of C + + 11   }  

Delete a sprite in a container//Suppose you want to delete the last sprite in the container: Sp_vec.popback (); //hypothesis is to delete object Sp_vec.eraseobject (SP1) directly; //Assume that all objects in the container are deleted sp_vec.clear ();
Other Information//b Find objects in a container:
1, if do not know whether the container has SP3 this genie. This is how it works:
Sp_vec.contains (SP3);//Suppose there is. Returns true if not, returns false///2, the known container has SP3 this genie, want to get its position in the container:
int Pos_int=sp_vec.find (SP3);
The above method can get the position of SP3. But the return is actually the address of the iterator, and the result you get is probably 45214 and so on. Suppose you want to get a position that is normally needed:
int Pos_int=sp_vec.find (SP3)-sp_vec.begin ();
In addition to adding vectors, the map is added.

Defined in the "CCMap.h" header file for "Cocos2dx_root/cocos/base".
Template <class K, class v>  class Cc_dll Map; ocos2d::map<k,v> is an associative container that uses std::unordered_map as the underlying structure. Std::unordered_map is an associative container for storing value pairs that can retrieve the corresponding values at high speed through their keys.

With Unordered_map, the key is generally unique, while the value is corresponding to the key.

Inside Unordered_map, elements are unordered, they are accessed by the hash value of the key, and the time complexity of the access is constant. Super fast.

Before Cocos2d-x v3.0, the second sequential container cocos2d::ccdictionary was used. But it will be abandoned very soon.

So in the future use. You should try to use Cocos2d::map instead of cocos::ccdictionary.


Map Basic UseCreate a container//Create an associative container map, the first parameter is a string type key, the second parameter is the key value of the Sprite class
map<std::string,sprite*>sp_map;  Auto Sp1=sprite::create ("Closenormal.png");  Sp1->setposition (Point (100,100));  This->addchild (sp1,1);  

put an object into a containerSp_map.insert ("SP1", SP1);//The wizard is placed in the container, the first parameter is a keyremove elements from the containerbecause map is a collection of key-value pairs. So we can use the specified key to remove the corresponding value. Auto sp=sp_map.at ("SP1");//Get SP1 by key value
Other Functions
Auto sp2=sp_map.at ("SP1");//Remove SP1  sp_map.insert ("One", SP2) by key, and then deposit SP1 with three key values into map  sp_map.insert ("22 ", SP2);  Sp_map.insert ("SP2");  Auto _key=sp_map.keys (SP1);//obtain SP1 corresponding key value for (const auto&e: _key)  {    Cclog ("_key is%s", E.c_str ());// Output SP1 the corresponding key value (there are four). Each is: sp1,11,22,33)}  


the elements of a map object are key-value pairs, which means that each element consists of two parts: a key and the value associated by the key. such keys and key values form a pair class, its first element points to the key, and the second element is the element.
Auto find_sp = Sp_map.find ("10");//Find the pair type with key "10" through find (). Auto SP3 = find_sp->second;//key corresponding object  std::string find_str = find_sp->first;//key  cclog ("SP6 key value is%s", Find_str.c_str ());//Print out the key  sp4->runaction (Moveby::create (0.3f,point (200,0)));//Let SP6 do the exercise  

the significance of container presence is not limited to memory management, so we should try to adopt the container class provided by Cocos2d-x.
Object-oriented thinking is the object of everythingwhen we use the basic data types int, float and so on, sometimes we need to think of them as objects,For example, when storing things in a container, these primary data types cannot be stored, Cocos2d-x 3.0 provides Value,This is what the basic data types are used as objects. It is possible to pass in the primary data type when initializing,The original 2.x version number of Ccinteger, ccfloat these things have been discarded.
//Create the object on the stack value. In the constructor, passing in the value you want to initialize, the type of the passed-in value can be/*byte,integer,float,double,boolean,string,vector,map, int_key_map*/
//Create the object on the stack value. In the constructor, passing in the value you want to initialize, the type of the passed-in value can be
<pre name= "code" class= "CPP" >value val1 (5.21f); Value Val2 (true);//log is used in the same way as Cclog. GetDescription is to obtain descriptive narrative information, the return value is Stringlog ("Val1 ' description is%s", Val1.getdescription (). C_STR ());. Log ("Val2 ' description is%s", Val2.getdescription (). C_STR ()); Value Val3 ("3"),//as followed by the corresponding data type can be converted to the corresponding data type log ("Val3 =%d", Val3.asint ());


Shimen Main Friendship tips:
The sword is one, the world is the sword, all things can be driven by me 、、、

10, COCOS2DX 3.0 game development to find small three container: Vector, Map, Value

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.