Cocos2d-x-API-详解-CCArray

http://my.oschina.net/wangxuanyihaha/blog/133743



摘要:  详细地介绍了CCArray中常用的方法,和一些常见的陷阱

Cocos2d-x-API-详解-CCArray

CCArray-API的简单介绍

CCArray继承与CCObject类,只是提供了一个面向对象的封装类

创建

 /** 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);</span>

删除

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

查询

//正向遍历
CCARRAY_FOREACH(__array__, __object__)
//反向遍历
CCARRAY_FOREACH_REVERSE(__array__, __object__)

    /** Returns element count of the array */
unsigned int count();
/** Returns capacity of the array */
unsigned int capacity();
/** Returns index of a certain object, return UINT_MAX if doesn't contain the object */
unsigned int indexOfObject(CCObject* object);
/** Returns an element with a certain index */
CCObject* objectAtIndex(unsigned int index);
/** Returns last element */
CCObject* lastObject();
/** Returns a random element */
CCObject* randomObject();
/** Returns a Boolean value that indicates whether object is present in array. */
bool containsObject(CCObject* object);
/** @since 1.1 */
bool isEqualToArray(CCArray* pOtherArray);
// Adding Objects

注意不要在CCARRAY_FOREACH中对CCArray中的元素进行删除或添加。

CCArray中的陷阱

CCArray一般不会被添加到其他的类中,所以其引用计数为1,并且被设置为autorelease

所以,创建的CCArray对象一定要retain,并在其析构方法中调用release释放内存

因此:

初始化

CCArray *tmpArray = CCArray::array();
tmpArray->retain();

增加元素

tmpArray>addObject(元素);

删除

tmpArray->removeAllObjects();
tmpArray->release();

cocos2d-x里,所有autorelease的对象只能在一个event loop里保持有效,也就是一次渲染frame,当每帧渲染结束时,pool manager会对他管理的每个autorelease的对象做一次release()调用,cocos2d-x里的代码很明确:

void CCDisplayLinkDirector::mainLoop(void)
{
    ...
         // release the objects
         CCPoolManager::sharedPoolManager()->pop();        
     ...
}

void CCPoolManager::pop()
{
    ...
    m_pCurReleasePool->clear();
    ...

所以,如果要想让生成的CCArray长久有效,就得调一次retain操作,在不需要的时候调release。

CCArray查询得到CCObject对象后怎么转型

在Lua脚本中

举例如下:

local str = arrbjectAtIndex(0)
str = tolua.cast (str, "CCString")
在C++中

举例如下:

(static_cast<cocos2d::CCSprite*>item)->getPosition();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值