Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating actions with Cocos2d-x


Creating actions with Cocos2d-x is a very simple process:

  1. Inside our createActions method, we will instantiate the actions we can use repeatedly in our game. Let's create our first actions:

    void GameLayer::createActions() {
     //swing action for health drops
     auto easeSwing = Sequence::create(
     EaseInOut::create(RotateTo::create(1.2f, -10), 2),
     EaseInOut::create(RotateTo::create(1.2f, 10), 2),
     nullptr);//mark the end of a sequence with a nullptr
     _swingHealth = RepeatForever::create( (ActionInterval *) easeSwing );
     _swingHealth->retain();
  2. Actions can be combined in many different forms. Here, the retained _swingHealth action is a RepeatForever action of Sequence that will rotate the health sprite first one way, then the other, with EaseInOut wrapping the RotateTo action. RotateTo takes 1.2 seconds to rotate the sprite first to -10 degrees and then to 10. And the easing has a value of 2, which I suggest you experiment with to get a sense...