Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Easing actions


Easing is animating with a specified acceleration to make the animations smooth. Ease actions are a good way to fake physics in your game. If you use easing actions with your animations, your game looks more natural with smoother animations.

How to do it...

Let's move a Sprite object from (200,200) to (500,200) with acceleration and deceleration.

auto sprite = Sprite::create("res/run_01.png");
sprite->setPosition(Vec2(200, 200));
this->addChild(sprite);

auto move = MoveTo::create(3.0f, Vec2(500, 200));
auto ease = EaseInOut::create(move, 2.0f);
sprite->runAction(ease);

Next, let's drop a Sprite object from the top of the screen and make it bounce.

auto sprite = Sprite::create("res/run_01.png");
sprite->setPosition(Vec2(size.width/2, size.height));
sprite->setAnchorPoint(Vec2(0.5f, 0.0f));
this->addChild(sprite);

auto drop = MoveTo::create(3.0f, Vec2(size.width/2, 0));
auto ease = EaseBounceOut::create(drop);
sprite->runAction(ease);

How it works...

The animation...