Time for action – creating our object pools
The pools are just vectors of objects. And here are the steps to create them:
Inside the
createPools
method, we first create a pool for meteors:void GameLayer::createPools() { int i; _meteorPoolIndex = 0; for (i = 0; i < 50; i++) { auto sprite = Sprite::createWithSpriteFrameName("meteor.png"); sprite->setVisible(false); _gameBatchNode->addChild(sprite, kMiddleground, kSpriteMeteor); _meteorPool.pushBack(sprite); }
Then we create an object pool for health packs:
_healthPoolIndex = 0; for (i = 0; i < 20; i++) { auto sprite = Sprite::createWithSpriteFrameName("health.png"); sprite->setVisible(false); sprite->setAnchorPoint(Vec2(0.5f, 0.8f)); _gameBatchNode->addChild(sprite, kMiddleground, kSpriteHealth); _healthPool.pushBack(sprite); }
We'll use the corresponding pool index to retrieve objects from the vectors as the game progresses.
What just happened?
We now have a vector of invisible meteor sprites and a...