0
votes

I pretty much finished my LibGDX project and now I'm just adding user-friendliness. I have a Texture (also placed in a sprite) that I would like to fade in and fade out repeatedly (NOT fast blinking). It's just rectangular funky-text that says "Touch to Start".

  • I considered making an animation of 6 or so pictures with varying opacity and just keep changing slides. Is this the best way to go?
  • I'm also looking for a libGDX effect that controls the transparency to avoid all the overhead and not make my animation choppy.

Can't think of any relevant code to add, Thanks for your help

enter image description here

EDIT

    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(touchToStartImage, screenWidth / 2 - touchToStartImage.getWidth() / 2, screenHeight / 2 - touchToStartImage.getHeight() / 2);
    elapsed += Gdx.graphics.getDeltaTime();
    blinkFontCache.setAlphas(Interpolation.fade.apply((elapsed / 0.01f) % 1f));
    blinkFontCache.draw(batch);
    blinkFontCache.translate(2f, 2f);
    batch.end();

I also defined blinkFontCache = new BitmapFontCache(numberPrinter); where numberPrinter is bitmapfont that is supposed to draw text. I've read the API guide for Interpolation and blinkFontCache, but unfortunately with the above I do not notice any change in the screen I have. Thanks

SOLUTION

EDIT with INTERPOLATION

    elapsed += Gdx.graphics.getDeltaTime();
    touchToStartSprite.setAlpha(Interpolation.fade.apply((elapsed / FADE_TIME) % 1f));

    blinker.begin();
    touchToStartSprite.draw(batch);
    blinker.end();

EDIT with ACTIONS

definitions

text = new Image(highScoreImage);
        text.addAction(Actions.alpha(0));
        text.act(0);
        text.addAction(Actions.forever(Actions.sequence(Actions.fadeIn(FADE_TIME), Actions.fadeOut(FADE_TIME))));

render()

    blinker.begin();
    text.act(Gdx.graphics.getDeltaTime());
    text.draw(blinker, 1);
    blinker.end();
2
Are you using stage2d? - Tenfour04
Oops sorry only Box2D that's good info - Script Kitty

2 Answers

2
votes

You could use the Image class from scene2d, which is an actor that can take a texture region and gives you several methods that can be useful. Here's an implementation.

 Image text = new Image(clickToStartRegion);
 Float fadeTime = 1f;

 //...

 text.addAction(Actions.alpha(0)); //make the text transparent.
 text.act(0); //update the text once
 text.addAction(Actions.sequence(Actions.fadeIn(fadeTime), Actions.fadeOut(fadeTime));

 //...

 text.act(deltaTime);

 //...

 text.draw(batch, 1);
1
votes

You can use the Interpolation class for the alpha. Assuming you're using a Sprite to draw this:

private float elapsed;
private static final float FADE_TIME = 1f; //time between blinks

//...

elapsed += deltaTime;
sprite.setAlpha(Interpolation.fade.apply((elapsed / FADE_TIME) % 1f));

//...

spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();