Game Development Reference
In-Depth Information
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
Anima-
tion
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 steps are repeated to create the other explosion animation used when the
player hits a meteor or a health pack.
animation = Animation::create();
for(int i = 1; i <= 7; i++) {
auto name =