博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CCArray
阅读量:4697 次
发布时间:2019-06-09

本文共 6302 字,大约阅读时间需要 21 分钟。

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

12/** 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  , 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
(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);1617 CCArray* targetsToDelete =new CCArray;1819 CCARRAY_FOREACH(_targets, jt)20 {21 CCSprite *target = dynamic_cast
(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);2728 // if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))29 if (projectileRect.intersectsRect(targetRect))30 {31 targetsToDelete->addObject(target);32 }33 }3435 CCARRAY_FOREACH(targetsToDelete, jt)36 {37 CCSprite *target = dynamic_cast
(jt);38 _targets->removeObject(target);39 this->removeChild(target, true);40 }4142 if (targetsToDelete->count() >0)43 {44 projectilesToDelete->addObject(projectile);45 }46 targetsToDelete->release();47 }4849 CCARRAY_FOREACH(projectilesToDelete, it)50 {51 CCSprite* projectile = dynamic_cast
(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) :
12for(int w = 0; w<100; w++){3                for(id object in arrayNS){4                        //Do something5                }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

转载于:https://www.cnblogs.com/Jzong/archive/2013/04/22/cocos2d-x.html

你可能感兴趣的文章
CROC-MBTU 2012, Elimination Round (ACM-ICPC) E. Mishap in Club
查看>>
爬虫抓取表格中的数据
查看>>
redis-3.0.0_rc5的RPM包制定
查看>>
JQuery Ajax 全解析
查看>>
Jfinal集成Spring
查看>>
JQuery操作class
查看>>
【easy】101. Symmetric Tree
查看>>
Android 4.0 NDK Updated
查看>>
第六次评分
查看>>
SAP FI配置步骤
查看>>
BZOJ 2288: 【POJ Challenge】生日礼物 优先队列+贪心+链表
查看>>
Basic Level 1006. 换个格式输出整数 (15)
查看>>
VC++6.0安装步骤
查看>>
josephus问题
查看>>
jedate-开始使用一款好用的时间插件
查看>>
【福利】微信小程序130个精选Demo合集
查看>>
Kubernetes之RBAC
查看>>
Fiddler实现IOS手机抓取https报文
查看>>
如何使用SubtitleWorkshop制作字幕
查看>>
Linux离线同步时间
查看>>