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 – updating our rocket sprite


The game's main loop will call the rocket's update method in every iteration.

  1. Inside the empty update method in Rocket.cpp, add the following lines:

    Point position = this->getPosition();
    if (_rotationOrientation == ROTATE_NONE) {
      position.x += _vector.x * dt;
      position.y += _vector.y * dt;
    } else {
      float angle = _angularSpeed * dt;
      Point rotatedPoint = position.rotateByAngle(_pivot, angle);
      position.x = rotatedPoint.x;
      position.y = rotatedPoint.y;
      float rotatedAngle;
      
      Point diff = position;
      diff.subtract(_pivot);
      Point clockwise = diff.getRPerp();
      
      if (_rotationOrientation == ROTATE_COUNTER) {
        rotatedAngle = atan2 (-1 * clockwise.y, -1 * clockwise.x);
      } else {
        rotatedAngle = atan2 (clockwise.y, clockwise.x);
      }
             
      _vector.x = _speed * cos (rotatedAngle);
      _vector.y = _speed * sin (rotatedAngle);
      this->setRotationFromVector();
      
      if (this->getRotation() > 0) {
        this->setRotation...