Game Development Reference
In-Depth Information
Time for action - coding the main loop
Finally, it's time for the last part in our logic.
1. Inside GameLayer.cpp :
void GameLayer::update(float dt) {
if (!_running) return;
if (_player->getPositionY() <
-_player->getHeight() ||
_player->getPositionX() < -_player->getWidth()
* 0.5f) {
_running = false;
}
If the _player object is off screen, we stop the game.
2. Now update all the elements, positions and check for collision:
_player->update(dt);
_terrain->move(_player->getVector().x);
if (_player->getState() != kPlayerDying)
_terrain->checkCollision(_player);
_player->place();
3. Move _gameBatchNode in relation to the _player object:
if (_player->getNextPosition().y > _screenSize.height
* 0.6f) {
_gameBatchNode->setPositionY(
(_screenSize.height * 0.6f -
_player->getNextPosition().y) * 0.8f);
Search WWH ::




Custom Search