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 – 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);
    
        } else {
            _gameBatchNode->setPositionY  ( 0 );
        }
  4. Make the game more difficult as...