O'Reilly logo

Cocos2d-x by Example : Beginner's Guide - Second Edition by Roger Engelbert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – initializing our Terrain class

We'll go over these steps next:

  1. The first important method to implement is initTerrain:
    void Terrain::initTerrain () {
        
        _increaseGapInterval = 5000;
        _increaseGapTimer = 0;
        _gapSize = 2;
        
        //init object pools
        for (int i = 0; i < 20; i++) {
              auto block = Block::create();
              this->addChild(block);
              _blockPool.pushBack(block);
        }
    
       _minTerrainWidth = _screenSize.width * 1.5f;
       
        random_shuffle(_blockPattern.begin(), _blockPattern.end());
        random_shuffle(_blockWidths.begin(), _blockWidths.end());
        random_shuffle(_blockHeights.begin(), _blockHeights.end());
       
       this->addBlocks(0);
    }

    We have a timer to increase the width of gaps (we begin with gaps two tiles long).

    We create a pool for blocks so we don't instantiate any during ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required