PuzzleSDK
SpineTest.cpp
浏览该文件的文档.
1/*******************************************************************************
2 * Copyright (c) 2013, Esoteric Software
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 ******************************************************************************/
25
26#include "SpineTest.h"
27#include <iostream>
28#include <fstream>
29#include <string.h>
30#include "spine/spine.h"
31
32using namespace cocos2d;
33using namespace std;
34using namespace spine;
35
36//------------------------------------------------------------------
37//
38// SpineTestScene
39//
40//------------------------------------------------------------------
41
42SpineTests::SpineTests()
43{
50}
51
53: _title("")
54{}
55
56std::string SpineTestLayer::title() const
57 {
58 return _title;
59 }
60
61// BatchingExample
63 if (!SpineTestLayer::init()) return false;
64
65 _title = "BatchingExample";
66
67 // Load the texture atlas.
68 _atlas = spAtlas_createFromFile("spine/spineboy.atlas", 0);
69 CCASSERT(_atlas, "Error reading atlas file.");
70
71 // This attachment loader configures attachments with data needed for cocos2d-x rendering.
72 // Do not dispose the attachment loader until the skeleton data is disposed!
73 _attachmentLoader = (spAttachmentLoader*)Cocos2dAttachmentLoader_create(_atlas);
74
75 // Load the skeleton data.
76 spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
77 json->scale = 0.6f; // Resizes skeleton data to 60% of the size it was in Spine.
78 _skeletonData = spSkeletonJson_readSkeletonDataFile(json, "spine/spineboy-ess.json");
79 CCASSERT(_skeletonData, json->error ? json->error : "Error reading skeleton data file.");
80 spSkeletonJson_dispose(json);
81
82 // Setup mix times.
83 _stateData = spAnimationStateData_create(_skeletonData);
84 spAnimationStateData_setMixByName(_stateData, "walk", "jump", 0.2f);
85 spAnimationStateData_setMixByName(_stateData, "jump", "run", 0.2f);
86
87 int xMin = _contentSize.width * 0.10f, xMax = _contentSize.width * 0.90f;
88 int yMin = 0, yMax = _contentSize.height * 0.7f;
89 for (int i = 0; i < 50; i++) {
90 // Each skeleton node shares the same atlas, skeleton data, and mix times.
91 SkeletonAnimation* skeletonNode = SkeletonAnimation::createWithData(_skeletonData, false);
92 skeletonNode->setAnimationStateData(_stateData);
93
94 skeletonNode->setAnimation(0, "walk", true);
95 skeletonNode->addAnimation(0, "jump", false, 3);
96 skeletonNode->addAnimation(0, "run", true);
97
98 skeletonNode->setPosition(Vec2(
99 RandomHelper::random_int(xMin, xMax),
100 RandomHelper::random_int(yMin, yMax)
101 ));
102 skeletonNode->setScale(0.8);
103 addChild(skeletonNode);
104 }
105
106 return true;
107}
108
110 // SkeletonAnimation instances are cocos2d-x nodes and are disposed of automatically as normal, but the data created
111 // manually to be shared across multiple SkeletonAnimations needs to be disposed of manually.
112 spSkeletonData_dispose(_skeletonData);
113 spAnimationStateData_dispose(_stateData);
114 spAttachmentLoader_dispose(_attachmentLoader);
115 spAtlas_dispose(_atlas);
116}
117
118// CoinExample
119
121 if (!SpineTestLayer::init()) return false;
122
123 _title = "CoinExample";
124
125 skeletonNode = SkeletonAnimation::createWithJsonFile("spine/coin-pro.json", "spine/coin.atlas", 1.f);
126 skeletonNode->setAnimation(0, "rotate", true);
127
128 skeletonNode->setPosition(Vec2(_contentSize.width / 2, 100));
129 addChild(skeletonNode);
130
131 return true;
132}
133
134// GoblinsExample
135
137 if (!SpineTestLayer::init()) return false;
138
139 _title = "GoblinsExample";
140
141 skeletonNode = SkeletonAnimation::createWithJsonFile("spine/goblins-pro.json", "spine/goblins.atlas", 1.5f);
142 skeletonNode->setAnimation(0, "walk", true);
143 skeletonNode->setSkin("goblin");
144
145 skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
146 skeletonNode->setScale(0.6);
147 addChild(skeletonNode);
148 return true;
149}
150
151// RaptorExample
152
154 if (!SpineTestLayer::init()) return false;
155
156 _title = "RaptorExample";
157 skeletonNode = SkeletonAnimation::createWithJsonFile("spine/raptor-pro.json", "spine/raptor.atlas", 0.5f);
158
159 skeletonNode->setAnimation(0, "walk", true);
160 skeletonNode->setAnimation(1, "empty", false);
161 skeletonNode->addAnimation(1, "gungrab", false, 2);
162
163 skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
164 skeletonNode->setScale(0.6);
165 addChild(skeletonNode);
166 return true;
167}
168
169// SpineboyExample
170
172 if (!SpineTestLayer::init()) return false;
173
174 _title = "SpineboyExample";
175 skeletonNode = SkeletonAnimation::createWithJsonFile("spine/spineboy-ess.json", "spine/spineboy.atlas", 0.6f);
176 skeletonNode->setStartListener( [] (spTrackEntry* entry) {
177 log("%d start: %s", entry->trackIndex, entry->animation->name);
178 });
179 skeletonNode->setInterruptListener( [] (spTrackEntry* entry) {
180 log("%d interrupt", entry->trackIndex);
181 });
182 skeletonNode->setEndListener( [] (spTrackEntry* entry) {
183 log("%d end", entry->trackIndex);
184 });
185 skeletonNode->setCompleteListener( [] (spTrackEntry* entry) {
186 log("%d complete", entry->trackIndex);
187 });
188 skeletonNode->setDisposeListener( [] (spTrackEntry* entry) {
189 log("%d dispose", entry->trackIndex);
190 });
191 skeletonNode->setEventListener( [] (spTrackEntry* entry, spEvent* event) {
192 log("%d event: %s, %d, %f, %s", entry->trackIndex, event->data->name, event->intValue, event->floatValue, event->stringValue);
193 });
194
195 skeletonNode->setMix("walk", "jump", 0.4);
196 skeletonNode->setMix("jump", "run", 0.4);
197 skeletonNode->setAnimation(0, "walk", true);
198 spTrackEntry* jumpEntry = skeletonNode->addAnimation(0, "jump", false, 1);
199 skeletonNode->addAnimation(0, "run", true);
200
201 skeletonNode->setTrackStartListener(jumpEntry, [] (spTrackEntry* entry) {
202 log("jumped!");
203 });
204
205 // skeletonNode->addAnimation(1, "test", true);
206 // skeletonNode->runAction(RepeatForever::create(Sequence::create(FadeOut::create(1), FadeIn::create(1), DelayTime::create(5), NULL)));
207
208 skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
209 skeletonNode->setScale(0.8);
210 addChild(skeletonNode);
211
212 scheduleUpdate();
213
214 return true;
215}
216
217void SpineboyExample::update (float deltaTime) {
218 // Test releasing memory.
219 // Director::getInstance()->replaceScene(SpineboyExample::scene());
220}
221
222// TankExample
223
225 if (!SpineTestLayer::init()) return false;
226
227 _title = "TankExample";
228 skeletonNode = SkeletonAnimation::createWithJsonFile("spine/tank-pro.json", "spine/tank.atlas", 0.5f);
229 skeletonNode->setAnimation(0, "drive", true);
230
231 skeletonNode->setPosition(Vec2(_contentSize.width / 2 + 400, 20));
232 skeletonNode->setScale(0.8);
233 addChild(skeletonNode);
234
235 return true;
236}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
spAttachmentLoader * _attachmentLoader
Definition: SpineTest.h:54
spSkeletonData * _skeletonData
Definition: SpineTest.h:55
virtual bool init()
Definition: SpineTest.cpp:62
spAtlas * _atlas
Definition: SpineTest.h:53
spAnimationStateData * _stateData
Definition: SpineTest.h:56
virtual bool init()
Definition: SpineTest.cpp:120
spine::SkeletonAnimation * skeletonNode
Definition: SpineTest.h:67
virtual bool init()
Definition: SpineTest.cpp:136
spine::SkeletonAnimation * skeletonNode
Definition: SpineTest.h:77
spine::SkeletonAnimation * skeletonNode
Definition: SpineTest.h:87
virtual bool init()
Definition: SpineTest.cpp:153
virtual std::string title() const
Definition: SpineTest.cpp:56
std::string _title
Definition: SpineTest.h:42
virtual bool init()
Definition: SpineTest.cpp:171
virtual void update(float deltaTime)
Definition: SpineTest.cpp:217
spine::SkeletonAnimation * skeletonNode
Definition: SpineTest.h:99
virtual bool init()
Definition: SpineTest.cpp:224
spine::SkeletonAnimation * skeletonNode
Definition: SpineTest.h:109