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 – moving and resetting


We move the terrain inside the move method.

  1. The move method receives as a parameter the amount of movement in the x axis:

    void Terrain::move (float xMove) {
        if (xMove < 0) return;
    
        if (_startTerrain) {
            
            if (xMove > 0 && _gapSize < 5) 
            _increaseGapTimer += xMove;
            
            if (_increaseGapTimer > _increaseGapInterval) {
                _increaseGapTimer = 0;
                _gapSize += 1;
            }
        }
        
        this->setPositionX(this->getPositionX() - xMove);
        
       auto  block = _blocks.at(0);  
       if (_position.x + block->getWidth() < 0) {
          auto firstBlock = _blocks.at(0);
          _blocks.erase(0);
          _blocks.pushBack(firstBlock);
          _position.x +=  block->getWidth();
          
          float width_cnt = this->getWidth() - block->getWidth() - ( _blocks.at(0))->getWidth();
          this->initBlock(block);
          this->addBlocks(width_cnt);
        }
    }

    The value for xMove...