Game Development Reference
In-Depth Information
Time for action - creating our object pools
The pools are just vectors of objects. And here are the steps to create them:
1. 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);
}
2. 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);
}
3. We'll use the corresponding pool index to retrieve objects from the vectors as the
game progresses.
Search WWH ::




Custom Search