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 – adding the main loop


It's inside our main loop that we update our b2World object.

  1. Start by updating the simulation as follows:

    void GameLayer::update(float dt) {
        
       if (!_running) return;
       if (_gameState == kGameOver) return;
        _world->Step(dt, 10, 10);
  2. Next, we need to determine if the game has finished by checking on the number of balls currently in play. We use the following for that:

    //track invisible objects
    for (auto ball : _balls) {
       if (!ball->isVisible() && ball->getInPlay()) {
            ball->setInPlay(false);
            ball->hide();
            //count down balls
            _ballsInPlay--;
            SimpleAudioEngine::getInstance()->playEffect("drop.wav");
            if (_ballsInPlay == 0) {
                _gameState = kGameOver;
                _gameOver->setVisible(true);
            }
        } else {
            ball->update(dt);
        }
    }
  3. Next, we continue to update the sprites as follows:

    if (!_cue->isVisible())  {
        _cue->hide();
    } else {
        _cue...