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

泪湿孤枕 提交于 2020-01-06 04:12:05

问题


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?


回答1:


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.




回答2:


You can use __Array instead of CCArray.

__Array * _leather;
__Array * _leatherSelection;

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

_leather->addObject(new Ref);
_leather->insertObject(<#cocos2d::Ref *object#>, <#ssize_t index#>);
_leather->getObjectAtIndex(<#ssize_t index#>);
_leather->getIndexOfObject(<#cocos2d::Ref *object#>);
_leather->getLastObject();


来源:https://stackoverflow.com/questions/23646731/object-pools-in-cocos2d-x-v3-0-final-deprecated-ccarray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!