Time for action – initializing our Blocks object
Finally, we will discuss the method that initializes the blocks based on our patterns
array:
So inside the
Terrain
class, we start theinitBlock
method as follows:void Terrain::initBlock(Block * block) { int blockWidth; int blockHeight; int type = _blockTypes[_currentTypeIndex]; _currentTypeIndex++; if (_currentTypeIndex == _blockTypes.size()) { _currentTypeIndex = 0; }
Begin by determining the type of building we are initializing. See how we loop through the
_blockTypes
array using the index stored in_currentTypeIndex
. We'll use a similar logic for the otherpatterns
arrays.Then, let's start building our blocks:
if (_startTerrain) { //... } else { _lastBlockHeight = 2; _lastBlockWidth = rand() % 2 + 2; block->setupBlock (_lastBlockWidth, _lastBlockHeight, type); }
The player must tap the screen to begin the game (
_startTerrain
). Until then, we show buildings with the same height (two tiles...