简体   繁体   English

Android LibGDX使纹理/文字“触摸以开始”闪烁

[英]Android LibGDX Make Texture/Text “Touch to Start” blink

I pretty much finished my LibGDX project and now I'm just adding user-friendliness. 我几乎完成了我的LibGDX项目,现在我只是添加了用户友好性。 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. 我考虑制作一张包含6张左右不透明度变化的图片的动画,并且只是不断更改幻灯片。 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. 我还在寻找一种libGDX效果,该效果可控制透明度以避免所有开销,并且不会使我的动画显得不稳定。

Can't think of any relevant code to add, Thanks for your help 无法想到要添加的任何相关代码,谢谢您的帮助

在此处输入图片说明

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); 我还定义了blinkFontCache = new BitmapFontCache(numberPrinter); where numberPrinter is bitmapfont that is supposed to draw text. 其中numberPrinter是应该绘制文本的bitmapfont。 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. 我已经阅读了Interpolation和blinkFontCache的API指南,但是不幸的是,我没有注意到屏幕上的任何变化。 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() render()

    blinker.begin();
    text.act(Gdx.graphics.getDeltaTime());
    text.draw(blinker, 1);
    blinker.end();

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. 您可以使用scene2d中的Image类,该类是可以获取纹理区域并为您提供有用的几种方法的actor。 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);

You can use the Interpolation class for the alpha. 您可以对Alpha使用Interpolation类。 Assuming you're using a Sprite to draw this: 假设您正在使用Sprite绘制此图像:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM