Should I just paste the code here? How will you get the spine asset for the player? Also thank you very much for responding so quickly 🙂
I am just going to paste the code, I also tried it with a different spine animation and it produced the same results.
public class SimpleTest1 extends ApplicationAdapter {
OrthographicCamera camera;
PolygonSpriteBatch batch;
SkeletonMeshRenderer renderer;
SkeletonRendererDebug debugRenderer;
TextureAtlas atlas;
Skeleton skeleton;
AnimationState state;
float rotation;
float accelerationY = -460;
float vx, vy, x = 50, y = 300;
float width;
float height;
public void create () {
camera = new OrthographicCamera();
batch = new PolygonSpriteBatch();
renderer = new SkeletonMeshRenderer();
renderer.setPremultipliedAlpha(true); // PMA results in correct blending without outlines.
debugRenderer = new SkeletonRendererDebug();
debugRenderer.setBoundingBoxes(false);
debugRenderer.setRegionAttachments(false);
atlas = new TextureAtlas(Gdx.files.internal("spine/player/skeleton.atlas"));
SkeletonJson json = new SkeletonJson(atlas); // This loads skeleton JSON data, which is stateless.
json.setScale(0.3f); // Load the skeleton at 30% the size it was in Spine.
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spine/player/skeleton.json"));
skeleton = new Skeleton(skeletonData); // Skeleton holds skeleton state (bone positions, slot attachments, etc).
AnimationStateData stateData = new AnimationStateData(skeletonData); // Defines mixing (crossfading) between animations.
state = new AnimationState(stateData); // Holds the animation state for a skeleton (current animation, time, etc).
state.setTimeScale(5f); /* When set this high the player finishes the animation and because of this he does not rotate.
When an animation is playing, such if I were set looping to true there is no issue with the player rotating. Set the TimeScale
to .5f and you will see he rotates just fine or set looping to true.
*/
state.setAnimation(0, "Moving", false);
/*I didn't find a method to get the scaled height or width and this seemed to work perfectly,
I bet some users would like to see this be added :)*/
width = skeleton.getData().getWidth() * .3f; // Multiply by the same scale set.... (.3)
height = skeleton.getData().getHeight() * .3f; // Multiply by the same scale set.... (.3)
}
public void render () {
if (Gdx.input.isKeyJustPressed(Keys.SPACE)) vy = 300;
state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
updatePlayer(Gdx.graphics.getDeltaTime()); // Update the player.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.updateWorldTransform(); // Uses the bones' local SRT to compute their world SRT.
// Configure the camera, PolygonSpriteBatch, and SkeletonRendererDebug.
camera.update();
batch.getProjectionMatrix().set(camera.combined);
debugRenderer.getShapeRenderer().setProjectionMatrix(camera.combined);
batch.begin();
renderer.draw(batch, skeleton); // Draw the skeleton images.
batch.end();
debugRenderer.draw(skeleton); // Draw debug lines.
}
public void updatePlayer(float delta) {
vy += accelerationY * delta;
y += vy * delta;
skeleton.setPosition(x, y);
// Rotate counterclockwise
// Adding to rotation moves left
if (vy > 0) {
rotation += 600 * delta;
if (rotation > 0) {
rotation = 0;
}
}
// Rotate clockwise
// Subtracting from rotation moves right
if (vy < -50) {
rotation -= 320 * delta;
if (rotation < -90) {
rotation = -90;
}
}
skeleton.getRootBone().getData().setRotation(rotation);
}
public void resize (int width, int height) {
camera.setToOrtho(false); // Update camera with new size.
}
public void dispose () {
atlas.dispose();
}
public static void main (String[] args) throws Exception {
new LwjglApplication(new SimpleTest1());
}
}