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 – retrieving objects from the pool


We just need to use the correct index to retrieve the objects from their respective vector:

  1. To retrieve meteor sprites, we'll use the resetMeteor method:

    void GameLayer::resetMeteor(void) {
       //if too many objects on screen, return
        if (_fallingObjects.size() > 30) return;
        
        auto meteor = _meteorPool.at(_meteorPoolIndex);
          _meteorPoolIndex++;
        if (_meteorPoolIndex == _meteorPool.size()) 
          _meteorPoolIndex = 0;
          int meteor_x = rand() % (int) (_screenSize.width * 0.8f) + _screenSize.width * 0.1f;
       int meteor_target_x = rand() % (int) (_screenSize.width * 0.8f) + _screenSize.width * 0.1f;
        
        meteor->stopAllActions();
        meteor->setPosition(Vec2(meteor_x, _screenSize.height + meteor->getBoundingBox().size.height * 0.5));
      
        //create action
        auto  rotate = RotateBy::create(0.5f ,  -90);
        auto  repeatRotate = RepeatForever::create( rotate );
        auto  sequence = Sequence::create (
        ...