2

I am animating some text in my libgdx application and would like a label text to fade-in and move (e.g. similar to this jsfiddle).

I can move, and change alpha of other objects (e.g. Sprites) and can move BitmapFontCaches. However I can't get alpha of the BitmapFontChage to change.

Declaration:

message = new BitmapFontCache(messageFont, true);
message.setWrappedText("some text", 10.0f, 10.0f, 10.0f);
message.setAlphas(0.0f);  

In my screen class, I override the render method, and call .draw() on a renderer class, which is (among other things) essentially just a message.draw(batch); call.

@Override
public void render(float delta) {
     ...
    try{
        batch.begin();

        feedbackRenderer.draw(batch);

        tweenManager.update(delta);}
    finally{
        batch.end();
    }
}

Then as a part of a timeline I call these two Tweens. (yes, they are wrapped in .push( ) and I do start my tweenManager:)

Tween.to(message, BitmapFontCacheAccessor.POSITION_X, animationDuration)
                        .target(35.0f)
Tween.to(message, BitmapFontCacheAccessor.ALPHA, animationDuration)
                            .target(1.0f)

The BitmapFontCacheAccessor tries to setAlphas() of the BitmapFontCache as such:

public class BitmapFontCacheAccessor implements TweenAccessor<BitmapFontCache> {

public static final int POSITION_X = 1;
public static final int ALPHA = 2;

@Override
public void setValues(BitmapFontCache target, int tweenType, float[] newValues) {
    switch (tweenType) {
        case POSITION_X:
            float y = target.getY();
            target.setPosition(newValues[0], y);
            break;
        case ALPHA:
            target.setAlphas(newValues[0]);
                break;}
}...

It moves the label (==> .setPosition(x, y) works!), but does not even touch the alpha. This exact same approach works for Sprites, which fade in nicely.

Is there perhaps some catch when tweening alpha for the BitmapFontCache? Is it possible?

Many thanks!

  • show the code where you draw the bitmapfontcache – Barodapride Jul 14 '15 at 14:12
  • Hey, I've added the code. It just got even wierder - after the fade-in tween, if I call a fade-out tween (e.g. going down from 1.0 to 0.0) it **fades out**!? I am not sure what to make of it. – Jano Horváth Jul 14 '15 at 14:31
  • Where is the part where you specifically render the font? – Barodapride Jul 14 '15 at 15:06
  • I'm not sure that I understand your comment. The piece I added, that is it. `message.draw(batch)` which gets called everytime the `render(float delta)` is called. – Jano Horváth Jul 15 '15 at 07:49
  • I guess I'd like to see what's inside the feedbackRenderer.draw(batch); – Barodapride Jul 15 '15 at 14:27
  • Sorry, I guess I am not explaining it well. .draw(batch) is a method I have defined myself. It calls .draw on all the elements I have on my screen. `public void draw(SpriteBatch batch){ drawImages(batch); drawMessage(batch); .... ` and drawMessage(batch) checks if message is not null and then calls `message.draw(batch)`. As simple as that. – Jano Horváth Jul 16 '15 at 09:44

1 Answers1

2

After a good hour of debugging I have found the reason for this funny behavior.

  1. Libgdx's BitmapFontCache does not have a getAlphas() method
  2. Therefore, to get the alpha channel I used getColor().a
  3. However, these two are not always synced. The behavior is quite random, I myself am not quite sure when it syncs and when it doesn't (f.ex. in the question above, the fade-outs would work, but fade-ins wouldn't)

The solution is to change and declare BOTH alpha channels.

Definition of BitmapFontCache:

message = new BitmapFontCache(messageFont, true);
message.setColor(1,1,1,0);
message.setAlphas(0);

and inside TweenAccessor:

case ALPHA:
//first alpha channel
   target.setAlphas(newValues[0]);
//second alpha channel
   Color c = target.getColor();
   c.a = newValues[0];
   target.setColor(c);

   break;

To you, hopeless SO wanderer, I address this answer so that you can spend some of the finite number of minutes of your life better than I did.