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*));
}

循环整个数组:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值