1
\$\begingroup\$

Let me preface by saying that I'm new to libgdx...

I have an animation that I'm currently drawing to the screen using:

game.batch.draw(flyAnimation2.getKeyFrame(elapsedTime, true), 200,200,0, 0, 124, 90, 1.0f, 1.0f, 90);

Where flyAnimation2 is: Animation flyAnimation2;

This works correctly. I now want to use UniversalTweenEngine to tween it from one side of the screen to the other.

I can't find any decent documentation on how to achieve this affect using an Animation. Any help would be appreciated!!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I would recommend representing the position of the animated sprite as a Vector2, then to use the universal tween engine you would first have to implement a TweenAccessor for that type;

public class Vector2Accessor implements TweenAccessor<Vector2> {
    public static final int TYPE_XY = 1;

    @Override
    public int getValues(Vector2 target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case TYPE_XY: 
        returnValues[0] = target.x; 
            returnValues[1] = target.y; 
            return 2;

            default: 
        assert false; 
        return -1;
        }
    }

    @Override
    public void setValues(Vector2 target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case TYPE_XY: 
        target.x = newValues[0];
            target.y = newValues[1]; 
        break;
            default: 
        assert false; 
        break;
        }
    }
}

After that it's a matter of registering the accessor, and updating the TweenManager with the delta time before rendering the Animation at the Vector2 position;

public class MyApplication extends ApplicationAdapter {

    private TweenManager manager = new TweenManager();
    private Vector2 position = new Vector2();

    public MyApplication() {
        Tween.registerAccessor(Vector2.class, new Vector2Accessor());
    }

    public void create() { 
        // Set the position to the left hand side of the screen, center vertically
        position.set(0, Gdx.graphics.getHeight() / 2.0f);
        Tween.to(position, Vector2Accessor.TYPE_XY, 5.0f).target(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() / 2.0f).start(manager);
    }

    public void render() {
       manager.update(Gdx.graphics.getDeltaTime());
       // For brevity I'll just pretend batch and flyAnimation and elapsedTime exists here
       batch.draw(flyAnimation2.getKeyFrame(elapsedTime, true), position.x,position.y, 0, 0, 124, 90, 1.0f, 1.0f, 90);
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .