23.cocos2d-x骨骼动画

前端之家收集整理的这篇文章主要介绍了23.cocos2d-x骨骼动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

温馨提示
Tests-V3.8\tests\cpp-tests\Resources\spine文件夹下有raptor.atlas、raptor.json、raptor.png文件


HelloWorldScene.h文件

  1. #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp,so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp文件

  1. #include "HelloWorldScene.h"
  2. #include "cocostudio/CocoStudio.h"
  3. #include "ui/CocosGUI.h"
  4. #include "spine\spine.h"
  5. #include <spine/spine-cocos2dx.h>
  6.  
  7. USING_NS_CC;
  8. using namespace spine;
  9. using namespace cocostudio::timeline;
  10.  
  11. Scene* HelloWorld::createScene()
  12. {
  13. // 'scene' is an autorelease object
  14. auto scene = Scene::create();
  15.  
  16. // 'layer' is an autorelease object
  17. auto layer = HelloWorld::create();
  18.  
  19. // add layer as a child to scene
  20. scene->addChild(layer);
  21.  
  22. // return the scene
  23. return scene;
  24. }
  25.  
  26. // on "init" you need to initialize your instance
  27. bool HelloWorld::init()
  28. {
  29. //////////////////////////////
  30. // 1. super init first
  31. if ( !Layer::init() )
  32. {
  33. return false;
  34. }
  35.  
  36. auto rootNode = CSLoader::createNode("MainScene.csb");
  37.  
  38. Vec2 visiable = Director::getInstance()->getVisibleSize();
  39. auto skeletonNode = new SkeletonAnimation("raptor.json","raptor.atlas");
  40. skeletonNode->setPosition(Vec2(visiable.x /2,50));
  41. skeletonNode->setScale(0.5);
  42. //0.一个一个执行,过度时不会卡顿
  43. //skeletonNode->setMix("walk","gungrab",0.5f);
  44. //1.轨道、走路动作、是否循环
  45. skeletonNode->setAnimation(0,"walk",true);
  46. ////2.setAnimation方法只能播放一种动画,所以当要连续播放不同的动画时,需要使用addAnimation方法来实现,它可以一条一条的播放不同的动画。
  47. skeletonNode->setAnimation(1,"empty",false);
  48. skeletonNode->addAnimation(1,"gungrab",false,2);
  49. //3.动画的播放快慢
  50. skeletonNode->setTimeScale(0.5f);
  51. //4.是否显示骨骼
  52. skeletonNode->setDebugBonesEnabled(true);
  53. addChild(skeletonNode);
  54.  
  55.  
  56. addChild(rootNode);
  57.  
  58. return true;
  59. }

猜你在找的Cocos2d-x相关文章