在使用cocos2d-X搜索真正的随机性时,不需要过多的条件,感兴趣的算法利用2 CCArray(s)在两个不同的对象池上分配和组合函数。
我们为一个物体“皮革”和“皮革选择”创建一个“池”。我们用一个名为“CCSprite”的自定义类覆盖SaddleManifold
/**
在标头上,使用不推荐的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有什么不同吗?
发布于 2014-05-14 07:19:19
Cocos2d-x附带了一个新的Vector,它取代了现在不再推荐的CCArray。(与代码相关的)主要区别是:
1)将矢量作为静态对象。您不需要声明指向它的指针:
class SpritesPool {
....
protected:
cocos2d::Vector<cocos2d::Sprite*> _leather;
cocos2d::Vector<cocos2d::Sprite*> _leatherSelection;
};
SpritesPool::SpritesPool() : _leather(5), _leatherSelection(4) {}2)向量类似于(并基于)正常的std::向量,那么所有已知的向量函数都是:
saddle = (Saddle *) _leatherArray.at(_leatherArrayIndex);
....
if (_leatherArrayIndex == _leatherArray.size()) {
_leatherArrayIndex =0;
}
...
_leatherSelection.pushBack(obstacle);您还可以从向量中选择一个随机对象。
saddle = (Saddle *) _leatherArray.getRandomObject(); 也许可以帮你实现。
发布于 2015-01-19 05:09:42
您可以使用__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();https://stackoverflow.com/questions/23646731
复制相似问题