Search code examples
cocos2d-iphonecocos2d-x

CCMutableArray deprecated, how to change code


I am working through the cocos2d-x SimpleGame project, and I am stuck in chapter 5, http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_5_-_How_to_Detect_the_Collisions.

What I found is that CCMutableArray is deprecated in favor of CCArray. But how do I need to modify the following, to make it work with CCArray (which apparently does not support templates)?

HelloWorldScene.h

cocos2d::CCMutableArray<cocos2d::CCSprite*> *_projectiles;

HelloWorldScene.cpp

// in init()
// Initialize arrays
_projectiles = new CCMutableArray<CCSprite*>;

HelloWorld::~HelloWorld()
{
  if (_targets)
  {
    _projectiles->release();
    _projectiles = NULL;
  }
}

HelloWorld::HelloWorld()
:_projectiles(NULL)
{
}

void HelloWorld::update(float dt)
{
  CCArray *projectilesToDelete = new CCArray<CCSprite*>;
  CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt;

  for (it = _projectiles->begin(); it != _projectiles->end(); it++)
  {
     CCSprite *projectile = *it;
     // (...)
  }
  // (...)
}

Solution

  • I think it is

    CCArray* array1 = CCArray::create();
    

    and later on to use it:

    CCObject* arrayItem;
    CCARRAY_FOREACH(array1, arrayItem){
        CCSprite* pItem = (CCSprite*)(arrayItem);
        //your code here
    }