CCArray Reference


引言:

    cocos2d完全支持CCArray这个类,这个类可以用来优化你的游戏。你可以在cocos2d/Support下面找到这个类的源代码,在cocos2d内部它跟apple的NSMutableArray类似,但是它比NSMutbaleArray这个类的效率更高。
    注意:CCArray和CCDictionary这两个类虽然可以hold住大多数的cocos2d-x类,但它们还没有强大到跟STL库一样,所以没必要把所有的STL类都替换成这两个类

CCArray只支持对象导向封装的类:

    CCArray是从CCObject类继承而来(CCObect类的主要目的是自动管理内存),它提供了一系列的接口:

与创建有关的:
/** Create an array */
static CCArray* create();
/** Create an array with some objects */
static CCArray* create(CCObject* pObject, …);
/** Create an array with one object */
 static CCArray* createWithObject(CCObject* pObject);
/** Create an array with capacity */
 static CCArray* createWithCapacity(unsigned int capacity);
/** Create an array with an existing array */
static CCArray* createWithArray(CCArray* otherArray);
与插入元素有关的:

/** Add a certain object */
void addObject(CCObject* object);
/** Add all elements of an existing array */
void addObjectsFromArray(CCArray* otherArray);
/** Insert a certain object at a certain index */
void insertObject(CCObject* object, unsigned int index);
与删除元素有关的:
 
 /** Remove last object */
void removeLastObject(bool bReleaseObj = true);
/** Remove a certain object */
void removeObject(CCObject* object, bool bReleaseObj = true);
/** Remove an element with a certain index */
void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
/** Remove all elements */
void removeObjectsInArray(CCArray* otherArray);
/** Remove all objects */
void removeAllObjects();
/** Fast way to remove a certain object */
void fastRemoveObject(CCObject* object);
/** Fast way to remove an element with a certain index */
void fastRemoveObjectAtIndex(unsigned int index);
有两个删除函数remove和fastRemove,相对fastRemove,remove会完全删除这个元素,而fastRemove只是释放这个元素,
它们之间有区别的代码段是:
unsigned int remaining = arr->num - index;
if(remaining>0)
{
    memmove((void *)&arr->arr[index], (void *)&arr->arr[index+1], remaining * sizeof(CCObject*));
}

循环整个数组:




你可能感兴趣的:(CCArray Reference)