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 the game


We already have the code that updates the countdowns inside the update. If it's time to add a meteor or a health pack we do it. If it's time to make the game more difficult to play, we do that too.

Note

It is possible to use an action for these timers: a Sequence action with a Delay action object and a callback. But there are advantages to using these countdowns. It's easier to reset them and to change them, and we can take them right into our main loop.

So it's time to add our main loop:

  1. What we need to do is check for collisions. So add the following code:

    if (_shockWave->isVisible()) {
     count = (int) _fallingObjects.size();
     for (i = count-1; i >= 0; i--) {
       auto sprite =  _fallingObjects.at(i);
       diffx = _shockWave->getPositionX() - sprite->getPositionX();
       diffy = _shockWave->getPositionY() - sprite->getPositionY();
       if (pow(diffx, 2) + pow(diffy, 2) <= pow(_shockWave->getBoundingBox().size.width * 0.5f, 2)) {
        sprite...