- Cocos2d-x Cookbook
- Akihiro Matsuura
- 469字
- 2021-07-09 20:58:49
Creating animations
When the characters in a game start to move, the game will come alive. There are many ways to make animated characters. In this recipe, we will animate a character by using multiple images.
Getting ready
You can create an animation from a series of the following image files:

You need to add the running girl's animation image files to your project and clean your project.
How to do it...
You can create an animation using a series of images. The following code creates the running girl's animation.
auto animation = Animation::create(); for (int i=1; i<=8; i++) { // from run_01.png to run_08.png std::string name = StringUtils::format("res/run_%02d.png", i); animation->addSpriteFrameWithFile(name.c_str()); } animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); animation->setLoops(10); auto action = Animate::create(animation); sprite->runAction(action);
How it works...
You can create an animation using the Animation
class and the Animate
class. They change multiple images at regular intervals. The names of the series image files have the serial number, we have added a file name to the Animation
class in the for loop. We can create the formatted string using the StringUtils
class in Cocos2d-x.
Tip
StringUtils
is a very useful class. The StringUtils::toString
method can generate the std::string
value from a variety of values.
int i = 100; std::string int_string = StringUtils::toString(i); CCLOG("%s ", int_string.c_str()); float j = 123.4f; std::string float_string = StringUtils::toString(j); CCLOG("%s", float_string.c_str());
StringUtils::format
method can generate the std::string
value using the printf
format.
You can view the log by using CCLOG macro. CCLOG is very useful. You can check the value of the variable in the log during the execution of your game. CCLOG has the same parameters as a sprintf
function.
We will add the file name into the Animation
instance using the addSpriteFrameWithFile
method. It sets the units of time which the frame takes using setDelayPerunit
method. It is set to restore the original frame when the animation finishes using the setRestoreOriginalFrame
method. True value is to restore the original frame. It is set to the number of times the animation is going to loop. Then, create the Animate
instance by passing it with the Animation
instance that you created earlier. Finally, run the runAction
method by passing in the Animate
instance.
If you want to run the animation forever, set -1
using the setLoops
method.
animation->setLoops(-1);
There's more...
In the preceding code, you cannot control each animation frame. In such cases, you can use the AnimationFrame
class. This class can control each animation frame. You can set the units of time the frame takes using the second argument of the AnimationFrame::create
method.
auto rect = Rect::ZERO; rect.size = sprite->getContentSize(); Vector<AnimationFrame*> frames; for (int i=1; i<=8; i++) { std::string name = StringUtils::format("res/run_%02d.png", i); auto frame = SpriteFrame::create(name.c_str(), rect); ValueMap info; auto animationFrame = AnimationFrame::create(frame, i, info); frames.pushBack(animationFrame); } auto animation = Animation::create(frames, 0.1f); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); animation->setLoops(-1); auto action = Animate::create(animation); sprite->runAction(action);
See also
- The Using a texture atlas recipe to create an animation using texture atlas
- Instant Node Package Manager
- Learning Java Functional Programming
- LabVIEW 2018 虛擬儀器程序設(shè)計(jì)
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- Mastering LibGDX Game Development
- Serverless架構(gòu)
- Visual Basic程序設(shè)計(jì)實(shí)踐教程
- Python深度學(xué)習(xí):模型、方法與實(shí)現(xiàn)
- C#開發(fā)案例精粹
- C++ Fundamentals
- Android Development Tools for Eclipse
- 軟件測(cè)試技術(shù)
- SQL Server 2008實(shí)用教程(第3版)
- Pandas入門與實(shí)戰(zhàn)應(yīng)用:基于Python的數(shù)據(jù)分析與處理
- AngularJS Web Application Development Cookbook