Time for action – initializing our Blocks

Finally the method that initializes the blocks based on our patterns array.

  1. Inside the Terrain class, we start the initBlock method like this:
    void Terrain::initBlock(Block * block) {
    
        int blockWidth;
        int blockHeight;
        
        int type = _blockTypes[_currentTypeIndex];
        _currentTypeIndex++;
    
        if (_currentTypeIndex == _blockTypes.size()) {
            _currentTypeIndex = 0;
        }

    We 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 other patterns arrays.

  2. Then let's start building our blocks:
    if (_startTerrain) { //... } else { _lastBlockHeight = 2; _lastBlockWidth = rand() % 2 + 2; block->setupBlock (_lastBlockWidth, ...

Get Cocos2d-x by Example Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.