SpineboyExample.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "SpineboyExample.h"
  30. #include "SkeletonRendererSeparatorExample.h"
  31. USING_NS_CC;
  32. using namespace spine;
  33. Scene* SpineboyExample::scene () {
  34. Scene *scene = Scene::create();
  35. scene->addChild(SpineboyExample::create());
  36. return scene;
  37. }
  38. bool SpineboyExample::init () {
  39. if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
  40. skeletonNode = SkeletonAnimation::createWithJsonFile("spineboy-pro.json", "spineboy.atlas", 0.6f);
  41. skeletonNode->setStartListener( [] (TrackEntry* entry) {
  42. log("%d start: %s", entry->getTrackIndex(), entry->getAnimation()->getName().buffer());
  43. });
  44. skeletonNode->setInterruptListener( [] (TrackEntry* entry) {
  45. log("%d interrupt", entry->getTrackIndex());
  46. });
  47. skeletonNode->setEndListener( [] (TrackEntry* entry) {
  48. log("%d end", entry->getTrackIndex());
  49. });
  50. skeletonNode->setCompleteListener( [] (TrackEntry* entry) {
  51. log("%d complete", entry->getTrackIndex());
  52. });
  53. skeletonNode->setDisposeListener( [] (TrackEntry* entry) {
  54. log("%d dispose", entry->getTrackIndex());
  55. });
  56. skeletonNode->setEventListener( [] (TrackEntry* entry, spine::Event* event) {
  57. log("%d event: %s, %d, %f, %s", entry->getTrackIndex(), event->getData().getName().buffer(), event->getIntValue(), event->getFloatValue(), event->getStringValue().buffer());
  58. });
  59. skeletonNode->setMix("walk", "jump", 0.4);
  60. skeletonNode->setMix("jump", "run", 0.4);
  61. skeletonNode->setAnimation(0, "walk", true);
  62. TrackEntry* jumpEntry = skeletonNode->addAnimation(0, "jump", false, 1);
  63. skeletonNode->addAnimation(0, "run", true);
  64. skeletonNode->setTrackStartListener(jumpEntry, [] (TrackEntry* entry) {
  65. log("jumped!");
  66. });
  67. // skeletonNode->addAnimation(1, "test", true);
  68. // skeletonNode->runAction(RepeatForever::create(Sequence::create(FadeOut::create(1), FadeIn::create(1), DelayTime::create(5), NULL)));
  69. skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
  70. addChild(skeletonNode);
  71. scheduleUpdate();
  72. EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
  73. listener->onTouchBegan = [this] (Touch* touch, cocos2d::Event* event) -> bool {
  74. if (!skeletonNode->getDebugBonesEnabled())
  75. skeletonNode->setDebugBonesEnabled(true);
  76. else if (skeletonNode->getTimeScale() == 1)
  77. skeletonNode->setTimeScale(0.3f);
  78. else
  79. Director::getInstance()->replaceScene(SkeletonRendererSeparatorExample::scene());
  80. return true;
  81. };
  82. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  83. return true;
  84. }
  85. void SpineboyExample::update (float deltaTime) {
  86. // Test releasing memory.
  87. // Director::getInstance()->replaceScene(SpineboyExample::scene());
  88. }