Search code examples
c++cocos2d-x

Object Pools in Cocos2D-X v3.0 Final - Deprecated CCArray


In the search for true randomness using cocos2d-X, without the need of excessive conditionals; the algorithm of interest utilizes 2 CCArray(s) to allocate and combine functions on two different object pools.

We Create a 'pool' for an object 'leather' and 'leatherSelection'. We Overwrite CCSprite with a custom class named 'SaddleManifold'

/**

On the header, using deprecated Cocos2D-x define an array of 'leather' types.

CCArray * _leather;
CCArray * _leatherSelection;

The use of CCArray is obviously unfit for the new cocos2D-x library. I am looking for ways to re-write my code using the new cocos2D-X V3 library which introduces vectors.

/** Destroyer **/
SaddleManifold::~SaddleManifold() { }

/**implementation class **/

_leather = CCArray::createWithCapacity(5);
_leather->retain();

 //Attempt to overload

_leatherSelection = CCArray::createWithCapacity(4);
_leatherSelection->retain();

  /** I get thrown an 'out-of-line' definition when building this signature **/
  void SaddleManifold::initSaddleManifold(Saddle * saddle) {

    .....}

Now if I try this:

/* Saddle is a Sprite! */

  Saddle * saddle;

  /*set boundaries*/
    while (currentSaddletWidth < _minSaddleManifoldWidth) {

For example: The saddle chooses randomly from an array of leather types; width parameters are embedded. Here's an excerpt of the code in question:

   saddle = (Saddle *) _leatherArray->objectAtIndex(_leatherArrayIndex);
    _leatherArrayIndex+;

    /**this does not run since it relies on that deprecated count()**/

    if (_leatherArrayIndex == _leatherArray> count()) {
        _leatherArrayIndex =0;
    }

    this->initSaddleManifold(saddle);

  /** width assignment differential **/
    currentSaddleWidth += saddle->getWidth();

    _leatherSelection->addObject(obstacle);

Which would be the best way to transition from CCArray to the new alternative? Is run-time any different than using CCArray?


Solution

  • Cocos2d-x comes with a new Vector class that replaces the now deprecated CCArray. Major differences (in relation with your code) are:

    1) Vector is used as a static object. You don't need to declare a pointer to it :

    class SpritesPool { 
       ....
       protected:
           cocos2d::Vector<cocos2d::Sprite*> _leather;
           cocos2d::Vector<cocos2d::Sprite*> _leatherSelection;
    };
    
    SpritesPool::SpritesPool() : _leather(5), _leatherSelection(4) {}
    

    2) Vector is similar (and based on) a normal std::vector then you have all the well known vector functions:

    saddle = (Saddle *) _leatherArray.at(_leatherArrayIndex); 
    ....
    if (_leatherArrayIndex == _leatherArray.size()) {
        _leatherArrayIndex =0;
    }
    ...
    _leatherSelection.pushBack(obstacle);
    

    You have also a method for pick a random object from the vector

    saddle = (Saddle *) _leatherArray.getRandomObject(); 
    

    that maybe can help you with your implementation.