对象池在Cocos2D-X v3.0最终 - 不推荐使用CCArray [英] Object Pools in Cocos2D-X v3.0 Final - Deprecated CCArray
问题描述
在使用cocos2d-X搜索真随机性时,不需要过多的条件;感兴趣的算法利用2个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.
我们为一个对象'皮革'和'皮革选择'。我们使用名为SaddleManifold的自定义类覆盖CCSprite
We Create a 'pool' for an object 'leather' and 'leatherSelection'. We Overwrite CCSprite with a custom class named 'SaddleManifold'
/ **
Cocos2D-x定义了一个皮革类型的数组。
On the header, using deprecated Cocos2D-x define an array of 'leather' types.
CCArray * _leather;
CCArray * _leatherSelection;
使用CCArray显然不适用于新的cocos2D-x库。我正在寻找方法重写我的代码使用新的cocos2D-X V3库,其中引入向量。
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);
这是从CCArray转换到新选择的最佳方式?运行时与使用CCArray有何不同?
Which would be the best way to transition from CCArray to the new alternative? Is run-time any different than using CCArray?
推荐答案
Cocos2d-x附带一个新的Vector类,替换现在已弃用的CCArray。主要区别(与您的代码相关)是:
Cocos2d-x comes with a new Vector class that replaces the now deprecated CCArray. Major differences (in relation with your code) are:
1)Vector用作静态对象。您不需要声明指向它的指针:
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是类似的(并基于)正常的std :: vector,那么你有所有知名的向量函数:
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.
这篇关于对象池在Cocos2D-X v3.0最终 - 不推荐使用CCArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!