SpineboyExample.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include "SpineboyExample.h"
  31. #include "GoblinsExample.h"
  32. #include <iostream>
  33. #include <fstream>
  34. #include <string.h>
  35. USING_NS_CC;
  36. using namespace spine;
  37. using namespace std;
  38. Scene* SpineboyExample::scene () {
  39. Scene *scene = Scene::create();
  40. scene->addChild(SpineboyExample::create());
  41. return scene;
  42. }
  43. bool SpineboyExample::init () {
  44. if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
  45. skeletonNode = SkeletonAnimation::createWithFile("spineboy.json", "spineboy.atlas", 0.6f);
  46. skeletonNode->startListener = [this] (int trackIndex) {
  47. spTrackEntry* entry = spAnimationState_getCurrent(skeletonNode->state, trackIndex);
  48. const char* animationName = (entry && entry->animation) ? entry->animation->name : 0;
  49. log("%d start: %s", trackIndex, animationName);
  50. };
  51. skeletonNode->endListener = [] (int trackIndex) {
  52. log("%d end", trackIndex);
  53. };
  54. skeletonNode->completeListener = [] (int trackIndex, int loopCount) {
  55. log("%d complete: %d", trackIndex, loopCount);
  56. };
  57. skeletonNode->eventListener = [] (int trackIndex, spEvent* event) {
  58. log("%d event: %s, %d, %f, %s", trackIndex, event->data->name, event->intValue, event->floatValue, event->stringValue);
  59. };
  60. skeletonNode->setMix("walk", "jump", 0.2f);
  61. skeletonNode->setMix("jump", "run", 0.2f);
  62. skeletonNode->setAnimation(0, "walk", true);
  63. spTrackEntry* jumpEntry = skeletonNode->addAnimation(0, "jump", false, 3);
  64. skeletonNode->addAnimation(0, "run", true);
  65. skeletonNode->setStartListener(jumpEntry, [] (int trackIndex) {
  66. log("jumped!", trackIndex);
  67. });
  68. // skeletonNode->addAnimation(1, "test", true);
  69. // skeletonNode->runAction(RepeatForever::create(Sequence::create(FadeOut::create(1), FadeIn::create(1), DelayTime::create(5), NULL)));
  70. Size windowSize = Director::getInstance()->getWinSize();
  71. skeletonNode->setPosition(Vector2(windowSize.width / 2, 20));
  72. addChild(skeletonNode);
  73. scheduleUpdate();
  74. EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
  75. listener->onTouchBegan = [this] (Touch* touch, Event* event) -> bool {
  76. if (!skeletonNode->debugBones)
  77. skeletonNode->debugBones = true;
  78. else if (skeletonNode->timeScale == 1)
  79. skeletonNode->timeScale = 0.3f;
  80. else
  81. Director::getInstance()->replaceScene(GoblinsExample::scene());
  82. return true;
  83. };
  84. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  85. return true;
  86. }
  87. void SpineboyExample::update (float deltaTime) {
  88. // Test releasing memory.
  89. // Director::getInstance()->replaceScene(SpineboyExample::scene());
  90. }