简体   繁体   English

使用libgdx,sin和java淡入淡出

[英]fadein fade out using libgdx , sin and java

I'm trying to create a function that make fadein and fadeout. 我正在尝试创建一个使淡入和淡出的功能。

I start using Math.sin() and batch.setColor() to fade the images to black, I don't know that this is the best approach but my idea is running a sin function. 我开始使用Math.sin()batch.setColor()将图像淡化为黑色,我不知道这是最好的方法,但是我的想法是运行一个sin函数。

I know that with sin function I can control my fadein/fideout time but I have 2 big problems: 我知道使用sin函数可以控制淡入/淡出时间,但是我有两个大问题:

  1. I need to start counting when senofloat == 0 and stop when its 1 or start on 1 and stop on 0 for fadeout, 我需要在senofloat == 0时开始计数,在其1时停止计数,或者从1开始并在0停止以淡出,
  2. I need control the number of times the cycles repeats, example 1 time for fade in 1 time for fade out 我需要控制循环重复的次数,例如1次淡入1次淡出

tempo is a global variable and tempo是一个全局变量,

tempo = Gdx.graphics.getDeltaTime();

This is what I have on the render function: 这是我在render函数上所拥有的:

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++;

Am I complicating this too much? 我会让事情变得复杂吗? Is there an easier approach? 有没有更简单的方法?

You can do this with Scene2D actions. 您可以使用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);
    //...
}

Call fadeOut or fadeIn to start a fade. 调用fadeOutfadeIn启动淡入。 You can change the Interpolation type to Interpolation.fade if you want. 您可以更改插补类型Interpolation.fade如果你想。 I think it looks better than sine for a color fade. 我认为它看起来比正弦的颜色褪色更好。

Best option and easier approach would be using universal-tween-engine 最好的选择和更简单的方法是使用通用补间引擎

very easy to use and effective as it comes from Aurelien Ribon 源自Aurelien Ribon,非常易于使用且有效

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

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