Time for action – adding the main loop
It's inside our main loop that we update our b2World
object.
- Start by updating the simulation as follows:
void GameLayer::update(float dt) { if (!_running) return; if (_gameState == kGameOver) return; _world->Step(dt, 10, 10);
- 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); } }
- Next, we continue to update the sprites as follows:
if (!_cue->isVisible()) { _cue->...