Question

I'm trying to animate a title moving in from the side - one Vector2 holds its intended final position and another holds its offset from that position (an offset of (0, 0) indicates the animation is finished). My debug calls are showing that the text arrives in the final position, but the Universal Tween Engine is for some reason setting the offset's value to (0, 0) immediately, rather than tweening it. Code and debug statements below:

Initialization of the TweenManager:

public void show() 
{       
    ...

    //Set up tween manager
    tweenManager = new TweenManager();
    Tween.registerAccessor(Button.class, new ButtonTweenAccessor());
    Tween.registerAccessor(Vector2.class, new Vector2TweenAccessor());
}

Starting the animation:

public void showLevel(int levelNumber)
{       
    previewTitle = levels[levelNumber].getTitle();
    previewTitlePos.y = GameConstants.getVirtualHeight() - this.TITLE_SPACE;
    previewTitlePos.x = (GameConstants.getVirtualWidth() / 2) - (font.getBounds(previewTitle).width / (2f * GameConstants.getPpuX()));
    titleOffset = new Vector2(GameConstants.getVirtualWidth(), 0);
    Gdx.app.log("Offset before anim starts:", previewTitlePos.toString());

    Tween.to(titleOffset, Vector2TweenAccessor.VALUE, 1)
        .target(.10f, 0.1f)
        .start(tweenManager);

    Gdx.app.log("Offset after tween.to is called:", previewTitlePos.toString());
}

VirtualWidth is the width of the window in the internal resolution of the game. I set the offset to this value originally to "hide" the text one screen length to the right offscreen.

And then the render method:

public void render(float delta) 
{       
    spriteBatch.begin();
    if(!previewTitle.isEmpty())
    {
        font.draw(spriteBatch, previewTitle, (previewTitlePos.x + titleOffset.x) * GameConstants.getPpuX(),  (previewTitlePos.y + titleOffset.y) * GameConstants.getPpuY());
        Gdx.app.log("Offset", titleOffset.toString());
    }
    spriteBatch.end();

    tweenManager.update(delta);
}

(I multiply the coordinates by the PpuX and PpuY values for resolution independence - I'm working at a single virtual resolution which is mapped to the actual window resolution).

Debug output:

Offset before anim starts:: [19.0:0.0]

Offset after tween.to is called:: [19.0:0.0]

Offset: [19.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

Offset: [0.0:0.0]

etc. for another 40 or so lines until the animation finishes. Does anybody see any errors in my code that would be causing this? Any help will be greatly appreciated.

Was it helpful?

Solution

Well, I figured it out - turns out the problem was in the one piece of code I didn't provide - the Vector2Accessor class used to interface with the tween engine. As a word of warning to other TweenEngine users out there - don't re-initialize the array in which you put the accessed values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top