Introduction to _ array containers and instances in Cocos2d-x

Source: Internet
Author: User
Tags addchild

_ Array class in the era of Cocos2d-x 2. x It is a ccarray class. It is designed to mimic the nsarray class in objective-C and manage the memory by referencing the count. _ Array inherits from the ref class, so it can accommodate the ref and the Object Pointer created by the subclass.


1. Create an _ array object
There are many functions to create the _ array object. The following is a summary of common functions:
? Static _ array * Create (). Create _ array.
? Static _ array * Create (ref * object ,...). Use a series of ref to create _ array.
? Static _ array * createwithobject (ref * object ). Use a ref to create _ array.
? Static _ array * createwithcapacity (unsigned int capacity ). Create _ array and set the capacity.
? Static _ array * createwith _ array (_ array * Other _ array ). Use an existing _ array to create another _ array.
? Static _ array * createwithcontentsoffile (const STD: string & pfilename ). Create _ array from the property list file.


2. Add Elements
Add elements to the _ array object must be the object pointer type of the ref and its subclass. The following is a summary of common functions:
? Void addobject (ref * object ). Add an element.
? Void addobjectsfromarray (_ array * otherarray ). Add all the elements in a _ array object to the current _ array object.
? Void insertobject (ref * object, ssize_t index ). Insert an element at the specified position. ssize_t is an alias of the int type.


3. Remove Elements
The following is a summary of commonly used functions for removing elements from the _ array container:
? Void removelastobject (). Remove the last element.
? Void removeobject (ref * object ). Removes an element.
? Void removeobjectatindex (ssize_t index ). Removes an element from a specified position.
? Void removeobjectsinarray (_ array * otherarray ). Removes an array _ array object.
? Void removeallobjects (). Remove all elements.
? Void fastremoveobject (ref * object ). Quickly remove an element.
? Void fastremoveobjectatindex (ssize_t index ). Quickly remove an element from a specified position.


4. Replacing and exchanging Elements
We can also use the following function to replace and exchange elements in the _ array container:
? Void exchangeobject (ref * object1, ref * object2 ). Exchange two elements.
? Void exchangeobjectatindex (ssize_t index1, ssize_t index2 ). Swap two specified position elements.
? Void replaceobjectatindex (ssize_t uindex, ref * object ). Replaces the specified position element with an object.


5. Other operation functions
In addition, there are many functions of the _ array object. Below is a summary of common functions:
? Ssize_t count (). Returns the number of elements.
? Ssize_t capacity (). Returns the capacity of _ array.
? Ssize_t indexofobject (ref * object ). Returns the position of the specified ref object pointer.
? Ref * objectatindex (ssize_t index ). Returns the ref object pointer at the specified position.
? Ref * lastobject (). Returns the last element.
? Ref * randomobject (). Returns a random element.
? Bool containsobject (ref * object ). Returns whether an element exists in the _ array container.
? Bool isequaltoarray (_ array * potherarray ). Determine whether _ array objects are equal.
? Void reverseobjects (). Invert the _ array container.


6. Traverse _ array container
The Cocos2d-x provides two macros for traversing the _ array container:
? Ccarray_foreach. Forward traversal.
? Ccarray_foreach_reverse. Reverse traversal.


Instance :__ array container
Next we will introduce the related functions in the _ array container through an example. Scenario: Click Go in the lower-right corner to add 100 genie to the scenario.

_ Array container instance



To implement the above functions, we do not use _ array. However, to familiarize ourselves with the _ array class, we specifically use the _ array class in this example.
Let's take a look at the code section. The helloworldscene. H code is as follows:

[HTML]View plaincopy
  1. # Ifndef _ helloworld_scene_h __
  2. # DEFINE _ helloworld_scene_h __
  3. # Include "cocos2d. H"
  4. # Define max_count 100 ①
  5. Class helloworld: Public cocos2d: Layer
  6. {
  7. Cocos2d ::__ array * List; ②
  8. Public:
  9. ~ Helloworld (); ③
  10. Static cocos2d: Scene * createscene ();
  11. Virtual bool Init ();
  12. Void menuclosecallback (cocos2d: ref * psender );
  13. Create_func (helloworld );
  14. };
  15. # Endif/_ helloworld_scene_h __



Line 1 of the above Code # define max_count 100 defines the macro max_count, which defines the number of genie generated at a time. The list of member variables declared by the Code in line ② _ array. The Code in line ③ declares the destructor. We need to release the member variable list in the destructor.
The init function code in helloworldscene. cpp is as follows:

[HTML]View plaincopy
  1. Bool helloworld: Init ()
  2. {
  3. If (! Layer: Init ())
  4. {
  5. Return false;
  6. }
  7. Size visiblesize = Director: getinstance ()-> getvisiblesize ();
  8. Vec2 origin = Director: getinstance ()-> getvisibleorigin ();
  9. Auto goitem = menuitemimage: Create (
  10. "Go-down.png ",
  11. "Go-up.png ",
  12. Cc_callback_1 (helloworld: menuclosecallback, this ));
  13. Goitem-> setposition (vec2 (origin. x + visiblesize. Width-goitem-> getcontentsize (). width/2,
  14. Origin. Y + goitem-> getcontentsize (). Height/2 ));
  15. Auto menu = menu: Create (goitem, null );
  16. Menu-> setposition (vec2: zero );
  17. This-> addchild (menu, 1 );
  18. This-> List = _ array: createwithcapacity (max_count); ①
  19. This-> list-> retain (); ②
  20. For (INT I = 0; I <max_count; ++ I ){
  21. Sprite * sprite = sprite: Create ("ball.png"); ③
  22. This-> list-> addobject (sprite); ④
  23. }
  24. Return true;
  25. }



Init is a function used to initialize the scenario. We created 100 genie in this function and put them in the List member variable of the _ array * type. The Code in line ① is to create a list member variable of the _ array * type and use the createwithcapacity function. The parameter is the initial capacity of the List container. Line ② code this-> list-> retain () is very important. The list container object created using the static function createwithcapacity is autorelease. If the retain () function is not called to keep the memory, when the init function ends, the list container object is automatically released, so that an error occurs when you use the list container object in other functions. The Code in line ③ is to create the genie object cyclically. The Code in line ④ adds the genie to the list container object. However, note that these genie objects have not been added to the scene, therefore, they do not appear when the scenario is displayed.
The menuclosecallback function code in helloworldscene. cpp is as follows:

[HTML]View plaincopy
  1. Void helloworld: menuclosecallback (ref * psender)
  2. {
  3. Log ("list-> count () = % d", this-> list-> count ());
  4. Size visiblesize = Director: getinstance ()-> getvisiblesize ();
  5. Ref * OBJ = nullptr;
  6. Ccarray_foreach (this-> list, OBJ) {①
  7. Sprite * sprite = (sprite *) OBJ; ②
  8. Int x = ccrandom_0_1 () * visiblesize. width; ③
  9. Int y = ccrandom_0_1 () * visiblesize. height; ④
  10. Sprite-> setposition (vec2 (x, y ));
  11. This-> removechild (sprite );
  12. This-> addchild (sprite );
  13. }
  14. }



This function is called after the player touches the go button. The first line of code uses the ccarray_foreach macro to traverse the data in the list container cyclically. The Code in line ② is sprite * sprite = (sprite *) OBJ to get the sprite object. The Code in line ③ ccrandom_0_1 () * visiblesize. width is the X axis coordinate randomly generated, and ccrandom_0_1 () is to generate 0 ~ Macro of the random number between 1. Similar to line 4, the Code randomly generates the Y axis coordinates of the genie.
The Destructor code in helloworldscene. cpp is as follows:

[HTML]View plaincopy
  1. Helloworld ::~ Helloworld ()
  2. {
  3. This-> list-> removeallobjects (); ①
  4. Cc_safe_release_null (this-> list); ②
  5. }



To release some resources in the destructor, line ① code this> List> removeallobjects () is to remove all objects in the list container, but this does not release the list container object itself. Line ② code cc_safe_release_null (this-> List) is the list container object that safely releases the member variable list. Cc_safe_release_null first releases the list container object and then assigns it to nullptr.

 

 

More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.cocoagame.net
For more exciting video courses, please follow the Cocos Course: http://v.51work6.com welcome to join the Cocos2d-x Technology Discussion Group: 257760386

Welcome to Zhijie IOS public classroom Platform

Introduction to _ array containers and instances in Cocos2d-x

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.