Internship Small white:: Cocos2d-x 3.0 Development (vii) Processing Cocostudio export animation in the program

Source: Internet
Author: User
Tags addchild

1. Overview

Using Cocostudio makes it easy to animate, and the next task is to use the animated animation in our program. In this article, I'm going to use a program to connect two animations together. There is a picture of the truth:

2. Making animations

To undertake the previous one, we will make an animation. The method of making animation with no difference before, not too familiar with the classmate can see: Cocos2d-x 3.0 Development (vi) use Cocostudio to create a skeletal animation. In the action list, right-click, add animation, and edit.

The end point of our newly created animation is coincident with the starting point of the animation in the previous article, so that the screen will not bounce when connected.

After making it, we'll export the animation.

3. Making UI

Since I can easily make the UI, I have made a UI that controls the animation playback. The production method was mentioned before. There's no difference. Use the UI editor to make the UI and export it.

4. Link to Project

Run the script to create our project, put the exported animation, the UI into the resource folder.

Then rewrite the Init method:

[CPP]View Plaincopyprint?
  1. BOOL Helloworld::init ()
  2. {
  3. //////////////////////////////  
  4. //1. Super init First
  5. if (! Layer::init ())
  6. {
  7. return false;
  8. }
  9. Size visiblesize = director::getinstance ()->getvisiblesize ();
  10. Point origin = Director::getinstance ()->getvisibleorigin ();
  11. Auto UI = dynamic_cast<layout*> (ccuihelper->createwidgetfromjsonfile ("Controlui.exportjson"));
  12. Ui->getchildbytag (ui_button_play1)->addtoucheventlistener (This, Toucheventselector (HelloWorld::  Touchcallback));
  13. Ui->getchildbytag (Ui_button_play2)->addtoucheventlistener (This, Toucheventselector (HelloWorld::  Touchcallback));
  14. Ui->getchildbytag (Ui_button_conn)->addtoucheventlistener (This, Toucheventselector (HelloWorld::  Touchcallback));
  15. Ui->getchildbytag (Ui_button_disconn)->addtoucheventlistener (This, Toucheventselector (HelloWorld::  Touchcallback));
  16. Auto Uilayer = Uilayer::create ();
  17. Uilayer->addwidget (UI);
  18. This->addchild (Uilayer);
  19. return true;
  20. }
  21. void Helloworld::touchcallback (object* obj,toucheventtype type)
  22. {
  23. //will Play
  24. }
5. Loading animations

The exported file for the animation is also a JSON. The load is encapsulated into a armature object. Armature is a subclass of Nodergba, so it can be addchild directly to the parent node. The method used in the Armaturemanager is loaded. It is a singleton that manages the armature in the entire scene. The animation we edited in the editor is animation, which is encapsulated in the armature. So this is a three-storey structure. Armaturemanager is the largest, then armature, and finally animation. We play the animation with the method in animation.

Having said the principle, let's take a look at the code. First, add the load armature in init.

[CPP]View Plaincopyprint?
    1. Armaturedatamanager::getinstance ()->addarmaturefileinfo ("Myanimation.exportjson");
    2. armature* armature = armature::create ("myanimation");
    3. Armature->settag (am_myanimation);
    4. Armature->setposition (Point (origin.x + VISIBLESIZE.WIDTH/2,
    5. ORIGIN.Y + VISIBLESIZE.HEIGHT/2));
    6. This->addchild (armature);

Then override the Touchcallback method to control the playback animation.

[CPP]View Plaincopyprint?
  1. void Helloworld::touchcallback (object* obj,toucheventtype type)
  2. {
  3. Auto UIBT = dynamic_cast<uibutton*> (obj);
  4. if (!UIBT)
  5. {
  6. return;
  7. }
  8. int tag = Uibt->gettag ();
  9. Auto armature = (armature*) getchildbytag (am_myanimation);
  10. switch (type)
  11. {
  12. Case toucheventtype::touch_event_ended:
  13. if (tag = = ui_button_play1)
  14. {
  15. Armature->getanimation ()->play ("hit");
  16. }
  17. Else if (tag ==ui_button_play2)
  18. {
  19. Armature->getanimation ()->play ("Fall");
  20. }
  21. Else if (tag = = ui_button_conn)
  22. {
  23. //will Conn
  24. }
  25. Else if (tag = = ui_button_disconn)
  26. {
  27. //will Dis Conn
  28. }
  29. Break ;
  30. Default:
  31. Break ;
  32. }
  33. }

6. Handling Animated Events

In Animation There is the concept of animated events, each beginning and ending of an animation event. All we need to do is listen to the event and write a response function for it.

So next we refine the Touchcallback function and add a listener function.

[CPP]View Plaincopyprint?
  1. //......
  2. else if (tag = = Ui_button_conn)
  3. {
  4. Armature->getanimation ()->setmovementeventcallfunc (this,movementevent_selector (HelloWorld::  Movementcallback));
  5. }
  6. else if (tag = = Ui_button_disconn)
  7. {
  8. Armature->getanimation ()->setmovementeventcallfunc (this,nullptr);
  9. }
  10. //......
  11. void Helloworld::movementcallback (armature * armature, Movementeventtype type, const char * name)
  12. {
  13. if (type = = complete)
  14. {
  15. if (strcmp (name,"Fall") = = 0)
  16. {
  17. armature* arm = (armature*) getchildbytag (am_myanimation);
  18. Arm->getanimation ()->play ("hit");
  19. }
  20. }
  21. }



Compile and run, you can see the animation is connected together.

7. Summary

load the animation by Armaturedatamanager the Singleton to associate it with the program. The listener of an animated event, which processes the behavior of the animation. Using these methods, we can flexibly use the animation created by Cocostudio.

Demo Download: http://download.csdn.net/detail/fansongy/6439225

This blog from the Asura road , reproduced please indicate the source, prohibited for commercial use: http://blog.csdn.net/fansongy/article/details/12955989


Internship Small white:: (EXT) Cocos2d-x 3.0 Development (vii) Processing Cocostudio export animation in the program

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.