Game Development Reference
In-Depth Information
Time for action - initializing our Blocks
object
Finally, we will discuss the method that initializes the blocks based on our
patterns
ar-
ray:
1. So inside the
Terrain
class, we start the
initBlock
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
_currentTypeIn-
dex
. We'll use a similar logic for the other
patterns
arrays.
2. 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) and random width: