CCArray是一个数据结构类,它对游戏存储数组型数据作了优化。在使用Cocosd-x开发游戏的过程当中CCArray使用频繁。数组
CCArray的建立
数据结构
// 建立一个数组 static CCArray* create(); // 使用一些对象建立数组,末尾加NULL标识 static CCArray* create(CCObject* pObject, ...); // 使用一个对象建立数组 static CCArray* createWithObject(CCObject* pObject); // 建立一个指定大小的数组 static CCArray* createWithCapacity(unsigned int capacity); // 使用一个现有的数组,建立出一个新数组 static CCArray* createWithArray(CCArray* otherArray);
CCArray添加元素优化
// 插入一个元素 void addObject(CCObject* object); // 插入另一个数组里面的所有对象 void addObjectsFormArray(CCArray* otherArray); // 在一个索引位置添加一个对象 void insertObject(CCObject* object, unsigned int index);
CCArray删除元素spa
// 移除最后的一个对象 void removeLastObject(bool bReleaseObj = true); // 移除一个肯定的对象 void removeObject(CCObject* object, bool bReleaseObj = true); // 移除一个肯定索引位置的元素 void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true); // 移除所有元素 void removeObjectsInArray(CCArray* otherArray); // 移除全部对象 void removeAllObjects(); // 快速移除一个对象 void fastRemoveObject(CCObject* object); // 快速移除一个肯定索引位置的对象 void fastRemoveObjectAtIndex(unsigned int index);
注意:code
[remove和fastRemove的区别]orm
remove是从CCArray中彻底的移除对象,fastRemove只是将CCArray中对应的对象释放,并无改变CCArray的结构。二者的区别就是在删除元素以后,是否把数组以后的元素向前移动覆盖掉以前位置的元素。对象
CCArray遍历元素索引
使用CCARRAY_FOREACH()快速遍历
游戏