SkeletonRendererSeparatorExample.cpp 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "SkeletonRendererSeparatorExample.h"
  30. #include "GoblinsExample.h"
  31. USING_NS_CC;
  32. using namespace spine;
  33. Scene* SkeletonRendererSeparatorExample::scene () {
  34. Scene *scene = Scene::create();
  35. scene->addChild(SkeletonRendererSeparatorExample::create());
  36. return scene;
  37. }
  38. bool SkeletonRendererSeparatorExample::init () {
  39. if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
  40. // Spineboy's back, which will manage the animation and GPU resources
  41. // will render only the front slots of Spineboy
  42. backNode = SkeletonAnimation::createWithJsonFile("spineboy-pro.json", "spineboy.atlas", 0.6f);
  43. backNode->setMix("walk", "jump", 0.4);
  44. backNode->setAnimation(0, "walk", true);
  45. backNode->setSlotsRange(backNode->findSlot("rear-upper-arm")->getData().getIndex(), backNode->findSlot("rear-shin")->getData().getIndex());
  46. backNode->setPosition(Vec2(_contentSize.width / 2, 20));
  47. // A simple rectangle to go between the front and back slots of Spineboy
  48. betweenNode = DrawNode::create();
  49. Vec2 rect[4];
  50. rect[0] = Vec2(0, 0);
  51. rect[1] = Vec2(40, 0);
  52. rect[2] = Vec2(40, 200);
  53. rect[3] = Vec2(0, 200);
  54. betweenNode->drawPolygon(rect, 4, Color4F(1, 0, 0, 1), 1, Color4F(1, 0, 0, 1));
  55. betweenNode->setPosition(Vec2(_contentSize.width / 2 + 30, 20));
  56. // Spineboy's front, doesn't manage any skeleton, animation or GPU resources, but simply
  57. // renders the back slots of Spineboy. The skeleton, animatio state and GPU resources
  58. // are shared with the front node!
  59. frontNode = SkeletonRenderer::createWithSkeleton(backNode->getSkeleton());
  60. frontNode->setSlotsRange(frontNode->findSlot("neck")->getData().getIndex(), -1);
  61. frontNode->setPosition(Vec2(_contentSize.width / 2, 20));
  62. // Add the front, between and back node in the correct order to this scene
  63. addChild(backNode);
  64. addChild(betweenNode);
  65. addChild(frontNode);
  66. scheduleUpdate();
  67. EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
  68. listener->onTouchBegan = [this] (Touch* touch, cocos2d::Event* event) -> bool {
  69. if (!backNode->getDebugBonesEnabled())
  70. backNode->setDebugBonesEnabled(true);
  71. else if (backNode->getTimeScale() == 1)
  72. backNode->setTimeScale(0.3f);
  73. else
  74. Director::getInstance()->replaceScene(GoblinsExample::scene());
  75. return true;
  76. };
  77. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  78. return true;
  79. }
  80. void SkeletonRendererSeparatorExample::update (float deltaTime) {
  81. // Test releasing memory.
  82. // Director::getInstance()->replaceScene(SpineboyExample::scene());
  83. }