Search code examples
javalibgdxtween

Universal TweenEngine several values at once


I'm interpolating two values at once with TweetEngine (X and Y position), but only X is updated...

Accessor class:

private static class PersonTweenAccessor implements TweenAccessor<Person> {

    public static final int POSITION_XY = 1;

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

            default:
                return -1;
        }
    }

    @Override
    public void setValues(Person target, int tweenType, float[] newValues) {
        switch(tweenType) {
            case POSITION_XY:
                target.position.set(newValues[0], newValues[1]);
                Gdx.app.log("position", newValues[0] + "," + newValues[1]);
                break;
        }
    }

}

Tween creation:

Gdx.app.log("Tween start", "From (" + position.x + "," + position.y + ") to (" + targetPoint.x + "," + targetPoint.y + ")");
Tween.to(this, PersonTweenAccessor.POSITION_XY, distance / speed)
                .target(targetPoint.x, targetPoint.y)
                .ease(TweenEquations.easeNone)
                .setCallback(positionTweenCallback)
                .start(GameWorld.tweenManager);

The output (of the log set in the setter) is this (trimmed to improve readability):

Tween start: From (50.0,20.0) to (283,25)
position: 51.305202,20.0
position: 52.14488,20.0
position: 53.2509,20.0
...
position: 280.70465,20.0
position: 281.80902,20.0
position: 282.85034,20.0
position: 283.0,25.0

As you can see, the first value is interpolated but the second is not, until the last access, where is set to its target value.


Solution

  • Your getValues method needs to return 2, not 1, because you have two values that you are modifying.