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


Time to bring the player to our party:

  1. Time to implement our onTouchBegan method. We'll begin by handling the two game states, intro and game over:

    bool GameLayer::onTouchBegan (Touch * touch, Event * event){
    
      //if game not running, we are seeing either intro or  //gameover
      if (!_running) {
        //if intro, hide intro message
        if (_introMessage->isVisible()) {
          _introMessage->setVisible(false);
    
          //if game over, hide game over message 
        } else if (_gameOverMessage->isVisible()) {
          SimpleAudioEngine::getInstance()->stopAllEffects();
          _gameOverMessage->setVisible(false);
          
        }
        
        this->resetGame();
        return true;
      }
  2. Here we check to see if the game is not running. If not, we check to see if any of our messages are visible. If _introMessage is visible, we hide it. If _gameOverMessage is visible, we stop all current sound effects and hide the message as well. Then we call a method called resetGame...