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


Let's implement our main update method.

  1. In GameLayer.cpp, inside the update method, add the following lines:

    if (!_running || _state != kGamePlay) return;
    if (_lineContainer->getLineType() != LINE_NONE) {
      _lineContainer->setTip (_rocket->getPosition() );
    }
    
    if (_rocket->collidedWithSides()) {
      _lineContainer->setLineType ( LINE_NONE );
    }
    _rocket->update(dt);
    
    //update jet particle so it follows rocket
    if (!_jet->isActive()) _jet->resetSystem();
    _jet->setRotation(_rocket->getRotation());
    _jet->setPosition(_rocket->getPosition());

    We check to see if we are not currently on pause. Then, if there is a line for our ship that we need to show in _lineContainer, we update the line's tip point with the _rocket current position.

    We run collision checks between _rocket and the screen sides, update the _rocket sprite, and position and rotate our _jet particle system to align it with the _rocket sprite.

  2. Next we update _comet (its...