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 – starting and restarting the game


Let's add the logic to start and restart the game.

  1. Let's write the implementation for resetGame:

    void GameLayer::resetGame(void) {
        _score = 0;
        _energy = 100;
        
        //reset timers and "speeds"
        _meteorInterval = 2.5;
        _meteorTimer = _meteorInterval * 0.99f;
        _meteorSpeed = 10;//in seconds to reach ground
        _healthInterval = 20;
        _healthTimer = 0;
        _healthSpeed = 15;//in seconds to reach ground
        
        _difficultyInterval = 60;
        _difficultyTimer = 0;
        
        _running = true;
        
        //reset labels
        _energyDisplay->setString(std::to_string((int) _energy) + "%");
        _scoreDisplay->setString(std::to_string((int) _score));
    }
  2. Next, add the implementation of stopGame:

    void GameLayer::stopGame() {
        
        _running = false;
        
        //stop all actions currently running
        int i;
        int count = (int) _fallingObjects.size();
        
        for (i = count-1; i >= 0; i--) {
            auto sprite = _fallingObjects...