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 – creating animations


Animations are a specialized type of action that require a few extra steps:

  1. Inside the same createActions method, add the lines for the two animations we have in the game. First, we start with the animation that shows an explosion when a meteor reaches the city. We begin by loading the frames into an Animation object:

    auto animation = Animation::create();
    int i;
    for(i = 1; i <= 10; i++) {
      auto name = String::createWithFormat("boom%i.png", i);
      auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name->getCString());
      animation->addSpriteFrame(frame);
    }
  2. Then we use the Animation object inside a Animate action:

    animation->setDelayPerUnit(1 / 10.0f);
    animation->setRestoreOriginalFrame(true);
    _groundHit = 
      Sequence::create(
        MoveBy::create(0, Vec2(0,_screenSize.height * 0.12f)),
        Animate::create(animation),
       CallFuncN::create(CC_CALLBACK_1(GameLayer::animationDone, this)), nullptr);
    _groundHit->retain();
  3. The same...