I've been searching a lot for a guide on how to implement 2D animations exported from Spine to Libgdx, but all I could find was outdated tutorials and source codes. I can handle basic Libgdx but Spine seems to be completely different. I was there, there, and also on lots of other sites (low reputation level won't let me post more links) but still I've got no idea how to do it. Does anyone know how to add those animations properly? (doesn't matter if it's json or binary) Preferably paste some code showing how to display something like spineboy so that I can see exactly what needs to be done. Any other suggestions appreciated..
2 Answers
Here is an official Spine 2D example where they load Spineboy into a game.
https://github.com/EsotericSoftware/spine-superspineboy
The following 4 lines are used to read a texture atlas and skeleton data as well as making some animation state data.
TextureAtlas playerAtlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy.atlas"));
SkeletonJson json = new SkeletonJson(playerAtlas);
SkeletonData playerSkeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/spineboy.json"));
AnimationStateData playerAnimationData = new AnimationStateData(playerSkeletonData);
You also need a spritebatch and a skeleton renderer object.
SpriteBatch batch = new SpriteBatch();
SkeletonRenderer skeletonRenderer = new SkeletonRenderer();
The skeleton data and animation state data can then be used to create a new Skeleton and Animation State object.
Skeleton skeleton = new Skeleton(playerSkeletonData);
AnimationState animationState = new AnimationState(playerAnimationData);
You then play animations like so.
animationState.setAnimation(0, "walk", true); // trackIndex, name, loop
Then each render call, you update the animation state with delta time and apply the animation state to the skeleton.
animationState.update(delta);
animationState.apply(skeleton);
After applying the animation state to the skeleton, you can now render the skeleton.
batch.begin();
skeletonRenderer.draw(batch, skeleton);
batch.end();
After update the animation and apply it to the skeleton I had to add one more line to the render loop
skeleton.updateWorldTransform();
batch.begin();
skeletonRenderer.draw(batch, skeleton);
batch.end();