gpt4 book ai didi

java - 使用 libgdx 、 sin 和 java 淡入淡出

转载 作者:行者123 更新时间:2023-12-01 09:46:24 24 4
gpt4 key购买 nike

我正在尝试创建一个淡入和淡出的函数。

我开始使用 Math.sin()batch.setColor() 将图像淡入黑色,我不知道这是最好的方法,但是我的想法是运行一个 sin 函数。

我知道使用 sin 函数我可以控制淡入/淡出时间,但我有 2 个大问题:

  1. 我需要在 senofloat == 0 时开始计数,在 1 时停止计数,或者从 1 开始计数,在 0 处停止淡出,
  2. 我需要控制循环重复的次数,例如 1 次淡入 1 次淡出

tempo 是一个全局变量,

tempo = Gdx.graphics.getDeltaTime();

这是我对渲染函数的了解:

float periudo=2;
float escala = 1;

senofloat = (float) (Math.sin(tempo*2*Math.PI/periudo)*(escala/2) + (escala/2));
batch.setColor(1, 1, 1, senofloat);

tempo++;

我是不是把事情搞得太复杂了?有没有更简单的方法?

最佳答案

您可以使用 Scene2D 操作来做到这一点。

private Actor actionManager = new Actor(); //use a Stage instead if using one anyway
private final Color fadeColor = new Color ();
private static final Color FADED = new Color(1,1,1,0);

private void fadeOut (float duration){
ColorAction colorAction = Actions.color(FADED, duration, Interpolation.sine);
colorAction.setColor(fadeColor);
actionManager.addAction(colorAction);
}

private void fadeIn (float duration){
ColorAction colorAction = Actions.color(Color.WHITE, duration, Interpolation.sine);
colorAction.setColor(fadeColor);
actionManager.addAction(colorAction);
}

public void render (){
//...

actionManager.act(Gdx.graphics.getDeltaTime());
batch.setColor(fadeColor);
//...
}

调用fadeOutfadeIn开始淡入淡出。如果需要,您可以将插值类型更改为 Interpolation.fade。我认为对于颜色褪色来说,它看起来比正弦波更好。

关于java - 使用 libgdx 、 sin 和 java 淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com