Convert CCMutableArray from previous Cocos2d-x version

Convert CCMutableArray from previous Cocos2d-x version
0.0 0


I upgrade the version of Cocos2d-x in my games, I used 1.12.0 and now I change to 2.0.1, I change a lots of code, it’s easy, but this part of my code I can’t change.
I know that I need to change CCMutableArray to CCArray, but I don’t know how to iterate using a CCArray

        CCMutableArray::CCMutableArrayIterator itEner;
    for (itEner = _energys->begin(); itEner != _energys->end(); itEner++) {
        CCSprite *energy = (CCSprite *) *itEner;
        if (!energy->getIsVisible())

        if (CCRect::CCRectIntersectsRect(_ship->boundingBox(),
                energy->boundingBox())) {


If anyone could help, I’ll be very grateful.


Yeah, the CCArray is even more confusing than CCMutableArray was And I ended up with lots of memory errors, the retain release mechanism is so confusing on the CCArray and the objects inside that I ended up writing a simple linked list class for some of my old CCMutableArray’s.

Anyway, I think you need something like (not tested):

    CCArray* objects = _energys->getObjects();
    CCSprite* spr = NULL;
    CCObject* pObj = NULL;

    CCARRAY_FOREACH(objects, pObj)
        spr = (CCSprite*)pObj;


        // do something


Using the example that you write, I could create one:

        CCObject* item;
    CCARRAY_FOREACH(_energys, item)
        CCSprite *energy = dynamic_cast(item);

            if (!energy->isVisible())

            if (CCRect::CCRectIntersectsRect(_ship->boundingBox(),
                    energy->boundingBox())) {

Thanks for the help.


Better use static_cast (or C-style cast) if you know the real type of object because it’s a compile-type operation.


You say instead of using:

CCSprite *energy = dynamic_cast(item);

I should can use:

CCSprite *asteroid = (CCSprite *)item_asteroids;

Is that right?




Thanks for the tip ‘redaur readur’.