【Cocos2d-x基础概念】CCArray

原创
2013/05/27 17:22
阅读数 3.3K

#Cocos2d-x-API-详解-CCArray

###CCArray-API的简单介绍

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

创建

<!-- lang: cpp -->
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);
 /** Create an array */

添加

<!-- lang: cpp -->
/** 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>

删除

<!-- lang: cpp -->
/** 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);

查询

<!-- lang: cpp -->
//正向遍历
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释放内存

因此:

初始化

<!-- lang: cpp -->
CCArray *tmpArray = CCArray::array();
tmpArray->retain();

增加元素

    <!-- lang: cpp -->
tmpArray>addObject(元素);

删除

<!-- lang: cpp -->
tmpArray->removeAllObjects();
tmpArray->release();

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

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

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

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

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

####在Lua脚本中

举例如下:

    <!-- lang: lua -->
local str = arrbjectAtIndex(0)
str = tolua.cast (str, "CCString")

####在C++中

举例如下:

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

详细可以看这里

展开阅读全文
加载中

作者的其它热门文章

打赏
0
4 收藏
分享
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部
返回顶部
顶部