Introduction
CCArray is a fully supported class of cocos2d. It is used for optimization in your game. You can find the CCArray class files in the cocos2d/Support group in the project. It’s used internally by cocos2d and is similar to Apple’s NSMutableArray class—except that CCArray performs better .
Note: CCArray and CCDictionary classes are used to hold most cocos2d-x classes but they still not as powerful as STL library. In that case there is no necessary to instead STL of CCArray and CCDictionary.
CCArray only provides an object-oriented wrapper class
CCArray is inherited from the CCObject class (CCObject's main purpose is automatic memory management), and provided a series of interface, including:
Create
1 /** Create an array */
2 static CCArray* create();
3 /** Create an array with some objects */
4 static CCArray* create(CCObject* pObject, …);
5 /** Create an array with one object */
6 static CCArray* createWithObject(CCObject* pObject);
7 /** Create an array with capacity */
8 static CCArray* createWithCapacity(unsigned int capacity);
9 /** Create an array with an existing array */
10 static CCArray* createWithArray(CCArray* otherArray);
Insert
1
2/** Add a certain object */
3 void addObject(CCObject* object);
4 /** Add all elements of an existing array */
5 void addObjectsFromArray(CCArray* otherArray);
6 /** Insert a certain object at a certain index */
7 void insertObject(CCObject* object, unsigned int index);
Delete
1
2/** Remove last object */
3 void removeLastObject(bool bReleaseObj = true);
4 /** Remove a certain object */
5 void removeObject(CCObject* object, bool bReleaseObj = true);
6 /** Remove an element with a certain index */
7 void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
8 /** Remove all elements */
9 void removeObjectsInArray(CCArray* otherArray);
10 /** Remove all objects */
11 void removeAllObjects();
12 /** Fast way to remove a certain object */
13 void fastRemoveObject(CCObject* object);
14 /** Fast way to remove an element with a certain index */
15 void fastRemoveObjectAtIndex(unsigned int index);
where there is remove and fastRemove, see the source code, remove is a relatively complete removed from the CCArray object fastRemove just will correspond in a CCArray element in a release from the code, main difference is that there are no speak delete elements after the elements to move forward over the removed element location, differential code is as follows:
1unsigned int remaining = arr->num - index;
2 if(remaining>0)
3{
4 memmove((void *)&arr->arr[index], (void *)&arr->arr[index+1], remaining * sizeof(CCObject*));
5}
Looping
In Step by Step Tutorials Chapter 5 - How to Detect the Collisions , The function update() below invoke CCARRAY_FOREACH(arr,obj)
is used to Loop the CCArray(_targets and _projectiles) to detect collisions every frame.
Declare it in HelloWorldScene.h and Define it in HelloWorldScene.cpp.
1
2void HelloWorld::update(ccTime dt)
3{
4 CCArray *projectilesToDelete = new CCArray;
5 CCObject* it = NULL;
6 CCObject* jt = NULL;
7
8 CCARRAY_FOREACH(_projectiles, it)
9 {
10 CCSprite *projectile = dynamic_cast<CCSprite*>(it);
11 CCRect projectileRect = CCRectMake(
12 projectile->getPosition().x - (projectile->getContentSize().width/2),
13 projectile->getPosition().y - (projectile->getContentSize().height/2),
14 projectile->getContentSize().width,
15 projectile->getContentSize().height);
16
17 CCArray* targetsToDelete =new CCArray;
18
19 CCARRAY_FOREACH(_targets, jt)
20 {
21 CCSprite *target = dynamic_cast<CCSprite*>(jt);
22 CCRect targetRect = CCRectMake(
23 target->getPosition().x - (target->getContentSize().width/2),
24 target->getPosition().y - (target->getContentSize().height/2),
25 target->getContentSize().width,
26 target->getContentSize().height);
27
28 // if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
29 if (projectileRect.intersectsRect(targetRect))
30 {
31 targetsToDelete->addObject(target);
32 }
33 }
34
35 CCARRAY_FOREACH(targetsToDelete, jt)
36 {
37 CCSprite *target = dynamic_cast<CCSprite*>(jt);
38 _targets->removeObject(target);
39 this->removeChild(target, true);
40 }
41
42 if (targetsToDelete->count() >0)
43 {
44 projectilesToDelete->addObject(projectile);
45 }
46 targetsToDelete->release();
47 }
48
49 CCARRAY_FOREACH(projectilesToDelete, it)
50 {
51 CCSprite* projectile = dynamic_cast<CCSprite*>(it);
52 _projectiles->removeObject(projectile);
53 this->removeChild(projectile, true);
54 }
55 projectilesToDelete->release();
56}
CCArray vs. NSArray
CCArray is faster but it also means objects in CCArray will change positions, so if you rely on a specific ordering of objects, you shouldn’t use the fastRemoveObject methods.
Speed Tests
Following code that is measured for CCArray and NSArray loops(200 elements):
- Test A (NSArray) :
1
2for(int w = 0; w<100; w++){
3 for(id object in arrayNS){
4 //Do something
5 }
6 }
- Test B (CCArray) :
1ccArray *arrayData = array->data;
2id object;
3int nu = arrayData->num;
4for(int w = 0; w<100; w++){
5 CCARRAY_FOREACH(arrayData, object){
6 object = arrayData->arr[i];
7 //Do something
8 }
9 }
10
The results
A mere 10% read access performance increase for CCArray compared to NSArray when using a for loop. And a tiny, negligible improvement when using the CCARRAY_FOREACH keyword compared to NSArray’s fast enumerator for(CCNode* node in array) to iterate over the array. Both these results are in the same ballpark with the C-Array, and I was pleasantly surprised to see the CCArray and NSArray all perform basically the same as the C-Array when using fast enumeration, with CCArray just a tiny fraction faster – exactly the same performance as a pure C-Array.
Use matters needing attention
CCArray generally will not be added to other classes, so its reference count is 1, and is set to autorelease. Creating the CCArray object to retain, and in its destructor method called release to release memory.
come from:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/CCArray