Time for action – creating animations
Animations are a specialized type of action that require a few extra steps:
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 anAnimation
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); }
Then we use the
Animation
object inside aAnimate
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();
The same...