Game Development Reference
In-Depth Information
What just happened?
We created two instances of a very special kind of action in Cocos2d-x: Animate . Here is
what we did:
• First, we created an Animation object. This object holds the references to all the
textures used in the animation. The frames were named in such a way that they
could easily be concatenated inside a loop ( boom1 , boom2 , boom3 , and so on).
There are 10 frames for the first animation and seven for the second.
• The textures (or frames) are SpriteFrame objects we grab from
SpriteFrameCache , which as you remember, contains all the information from
the sprite_sheet.plist data file. So the frames are in our sprite sheet.
• Then when all frames are in place, we determine the delay of each frame by divid-
ing the total amount of seconds we want the animation to last by the total number
of frames.
• The setRestoreOriginalFrame method is important here. If we set
setRestoreOriginalFrame to true , then the sprite will revert to its origin-
al appearance once the animation is over. For example, if I have an explosion an-
imation that will run on a meteor sprite, then by the end of the explosion anima-
tion, the sprite will revert to displaying the meteor texture.
• Time for the actual action. Animate receives the Animation object as its para-
meter. (In the first animation, we shift the position of the sprite just before the ex-
plosion appears, so there is an extra MoveBy method.)
• And in both instances, I make a call to an animationDone callback already im-
plemented in the class. It makes the calling sprite invisible:
void GameLayer::animationDone (Node* pSender) {
pSender->setVisible(false);
}
Note
We could have used the same method for both callbacks ( animationDone and
shockwaveDone ) as they accomplish the same thing. But I wanted to show you
a callback that receives as an argument, the node that made the call and one that
did not. Respectively, these are CallFuncN and CallFunc , and were used in-
side the action sequences we just created.
Search WWH ::




Custom Search