Cocos2D-X v3.0 Final中的对象池 - 已弃用的CCArray

时间:2014-05-14 06:00:33

标签: c++ cocos2d-x

在使用cocos2d-X搜索真正的随机性时,不需要过多的条件;感兴趣的算法利用2个CCArray在两个不同的对象池上分配和组合函数。

我们为对象'皮革'和'皮革选择'创建一个'池'。我们用名为'SaddleManifold'的自定义类覆盖CCSprite

/ **

在标题上,使用弃用的Cocos2D-x定义了一个'皮革'类型的数组。

CCArray * _leather;
CCArray * _leatherSelection;

CCArray的使用显然不适合新的cocos2D-x库。我正在寻找使用引入向量的新cocos2D-X V3库重新编写代码的方法。

/** 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) {

    .....}

现在,如果我试试这个:

/* Saddle is a Sprite! */

  Saddle * saddle;

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

例如:马鞍从一系列皮革类型中随机选择;宽度参数是嵌入的。以下是相关代码的摘录:

   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);

哪种方式是从CCArray过渡到新的替代方案?运行时是否与使用CCArray不同?

2 个答案:

答案 0 :(得分:2)

Cocos2d-x附带了一个新的Vector类,它取代了现在已弃用的CCArray。主要差异(与您的代码相关)是:

1)Vector用作静态对象。你不需要声明指向它的指针:

class SpritesPool { 
   ....
   protected:
       cocos2d::Vector<cocos2d::Sprite*> _leather;
       cocos2d::Vector<cocos2d::Sprite*> _leatherSelection;
};

SpritesPool::SpritesPool() : _leather(5), _leatherSelection(4) {}

2)Vector类似(并基于)一个普通的std :: vector,然后你就拥有了所有众所周知的向量函数:

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

您还有一种从矢量中选择随机对象的方法

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

可能可以帮助您实施。

答案 1 :(得分:0)

您可以使用__Array代替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();