Game Development Reference
In-Depth Information
Time for action - retrieving objects from
the pool
We just need to use the correct index to retrieve the objects from their respective vector:
1. To retrieve meteor sprites, we'll use the resetMeteor method:
void GameLayer::resetMeteor(void) {
//if too many objects on screen, return
if (_fallingObjects.size() > 30) return;
auto meteor = _meteorPool.at(_meteorPoolIndex);
_meteorPoolIndex++;
if (_meteorPoolIndex == _meteorPool.size())
_meteorPoolIndex = 0;
int meteor_x = rand() % (int) (_screenSize.width
* 0.8f) + _screenSize.width * 0.1f;
int meteor_target_x = rand() % (int)
(_screenSize.width * 0.8f) + _screenSize.width * 0.1f;
meteor->stopAllActions();
meteor->setPosition(Vec2(meteor_x,
_screenSize.height +
meteor->getBoundingBox().size.height * 0.5));
//create action
auto rotate = RotateBy::create(0.5f , -90);
auto repeatRotate = RepeatForever::create( rotate
);
auto sequence = Sequence::create (
MoveTo::create(_meteorSpeed,
Vec2(meteor_target_x, _screenSize.height * 0.15f)),
CallFunc::create(std::bind(&GameLayer::fallingObjectDone,
this, meteor) ), nullptr);
meteor->setVisible ( true );
meteor->runAction(repeatRotate);
Search WWH ::




Custom Search