Cocos2d-x Series 5 animation and action

Source: Internet
Author: User

1. Scenario Switching
Create an ImageScene class

#ifndef IMAGESCENE_H#define    IMAGESCENE_H#include <iostream>#include <cocos2d.h>USING_NS_CC;class ImageScene : public Layer {public:    virtual bool init() {        Sprite *s = Sprite::create("HelloWorld.jpg");        Size size = Director::getInstance()->getVisibleSize();        s->setPosition(Point(size.width / 2, size.height / 2));        addChild(s);        return true;    };    static Scene* createScene() {        Scene *s = Scene::create();        ImageScene *layer = ImageScene::create();        s->addChild(layer);        return s;    };    CREATE_FUNC(ImageScene);private:};#endif    /* IMAGESCENE_H */

Add a labelTTF to the initial scene and click it to switch to the next scenario (ImageScene)

Bool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size size Size = Director: getInstance ()-> getVisibleSize (); LabelTTF * label = LabelTTF :: create ("Show Next Scene", "Courier", 36); addChild (label); label-> setPosition (Point (size. width/2, size. height/2); EventListenerTouchOneByOne * listener = EventListenerTouchOneByOne: create (); // listener-> onTouchBegan = [label, size] (Touch * t, Event * e) {// Point p = t-> getLocation (); p-= Point (size. width/2, size. height/2); if (label-> getBoundingBox (). containsPoint (p) {log ("change scene"); ctor: getInstance ()-> replaceScene (ImageScene: createScene ();} return false ;}; Director:: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, label); // Add listener return true for label ;}

2. scene animation effect

When switching scenes, you often need to add some animations to make them look less sudden;

Director: getInstance ()-> replaceScene (ImageScene: createScene (); change it to the following code to add an animation During scenario switching;

Director: getInstance ()-> replaceScene (TransitionFadeBL: create (1, ImageScene: createScene (); // 1 indicates the animation duration is 1 s.

You can use APIs to view the effects of other scenarios;

3. Action in cocos2d-x
> 1. Action
If you want to change the location of a node, you need to use the Action in the cocos2d-x.

Label-> runAction (MoveTo: create (1, Point (100,100); // move to (100,100), 1 indicates the duration of the Action 1 s

The above method is MoveTo, which indicates moving to a certain point. Of course, a distance can also be moved, and the corresponding class is MoveBy;
In the cocos2d-x, the action can be reversed, let's look at the Code:

label->runAction(MoveBy::create(1, Point(50, 50))->reverse());

In the above Code, if there is no reverse, it indicates the distance to move (50, 50) to the upper right corner. However, after the reverse is added, the action changes to move (50, 50) to the lower left corner );

> 2. Repeated actions

Label-> runAction (Repeat: create (RotateBy: create (1,180), 4); // The action is repeated for four times. label-> runAction (RepeatForever: create (RotateBy:: create (1,180); // The action is always repeated.

> 3. Mixed action

auto actions = Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360), NULL);label->runAction(actions);        

The key to a hybrid action is to use a Spawn to package multiple actions and execute them together;

> 4. Sequential action

auto actions = Sequence::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360), NULL);label->runAction(actions);

The difference between a sequential action and a hybrid action is to replace Spawn with a Sequence and add the action to a Sequence for execution in Sequence;

> 5. Action listening

After an action is executed, some additional operations are often performed, just like the animation listening in android;

            auto actions = Sequence::create(                    MoveBy::create(1, Point(100, 100)),                    RotateBy::create(1, 360),                    CallFunc::create([]() {                        log("Action complete");                    })            , NULL); //            label->runAction(actions);

When creating an action, you can input a listener function that completes the action to listen to the action. The return value of this function is void and has no parameters;
CallFunc * CallFunc: create (const std: function <void ()> & func)

In the cocos2d-x, has encapsulated a lot of actions, in the actual use, can be based on these actions to combine a more dazzling action;


  4. Frame Animation

Auto cache = SpriteFrameCache: getInstance (); // get an animation cache instance cache-> addSpriteFramesWithFile ("anim. plist "); // Add a frame animation char name [15] From the file; memset (name, 0, 15); // Vector <SpriteFrame *> vec; // for (int I = 0; I <20; I ++) {sprintf (name, "anim % 04d", I); // vec. pushBack (cache-> getSpriteFrameByName (name);} Animation * anim = Animation: createWithSpriteFrames (vec, 0.1f); // create an Animation based on the SpriteFrame * set; animate * animate = Animate: create (anim); // create an animation Action from the animation; auto sprite = Sprite: create (); addChild (sprite ); sprite-> setPosition (200,200); sprite-> runAction (RepeatForever: create (animate ));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.