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 – handling touches


Let's go back to GameLayer.cpp and add our game's final touches (pun intended).

  1. First we work on our onTouchBegan method:

    bool GameLayer::onTouchBegan(Touch* touch, Event* event) {
        
        if (!_running) {
            
            if (_player->getState() == kPlayerDying) {
                _terrain->reset();
                _player->reset();
                resetGame();
            }
            return true;
        }

    If we are not running the game and the _player object died, we reset the game on touch.

  2. Next, if the terrain has not started, insert the following:

        if (!_terrain->getStartTerrain()) {
            _terrain->setStartTerrain ( true );
            return true;
        }

    Remember that at first the buildings are all the same height and there are no gaps. Once the player presses the screen, we begin changing that through setStartTerrain.

  3. We finish with:

        if (touch) {
          if (_player->getState() == kPlayerFalling) {
                _player->setFloating ( _player->getFloating...