Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – initializing our Blocks object


Finally, we will discuss the method that initializes the blocks based on our patterns array:

  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 _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, _lastBlockHeight, type);
    }

    The player must tap the screen to begin the game (_startTerrain). Until then, we show buildings with the same height (two tiles...