LibGDX - Fade In problem

Hi there,

I’m having a bit of a problem. I’m trying to get the graphics in my screen to fade in from black. The way I have it setup is as so:

Alpha = 0;
fadeIn = false;

(in create)
if(fadeIn == false) fadeIn();

(fadeIn() method)
fadeIn = true;

(render)
if(fadeIn == true) alpha = (float) (alpha + 0.02);
if(alpha == 1) alpha = 1;

The issue is that it just keeps fading in the resetting back to transparent, then fading back in, etc.

Easy way out, try using Actions.fadeIn

For example purposes, let’s say I’m using spritebatch.setColor(#,#,#,alpha); for this…

I’ve compressed it down to these 2 lines of code… but now it flickers once when done.
if(alpha < 1) alpha = (float) (alpha + 0.02);
else if(alpha >= 1) alpha = 1;

Like jimmt said, Actions.fadeIn is a nice way to use fades for actors or a stage (actually, actions are nice for lot’s of things).

If you don’t use actors, you can still benefit from the actions system, as an Actor is not an abstract class. Create an actor and assign it the initial color value and add a fade in action. Call act on the actor in your update/render method. This will step the actions you have put in. Then you can get the actors color and have the updated color. You can just use the alpha component of the color. Here is some example code of how you could use it:


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;

public class FadeTest {
	//Transparent color
	private static final Color TRANSPARENT = new Color(1f, 1f, 1f, 0f);
	
	private Actor fadeActor = new Actor();
	private SpriteBatch batch = new SpriteBatch();

	//Your init method
	public void create() {
		//Assign the starting value
		fadeActor.setColor(TRANSPARENT);
		// Fade in during 2 seconds
		fadeActor.addAction(Actions.fadeIn(2f));
	}

	//Your update and render method
	public void draw() {
		//Act updates the actions of an actor
		fadeActor.act(Gdx.graphics.getDeltaTime());

		//Get the current color of the actor
		batch.setColor(fadeActor.getColor());
		
		batch.begin();
		//Render stuff
		batch.end();
	}

}

It probably might be a bit overkill, but I like it because the actions enable you to easily do different transitions (even in parallel). So if you don’t know actors and actions, have a look at it.

[quote=“syszee,post:1,topic:51179”]
There are some big errors in this code. Here’s what I suppose you wanted to do:

if(fadeIn) {	// no need to write == true 
	alpha = (float) (alpha + 0.02 * delta);		// you need to factor in delta time since last frame or your fade will be different on faster/slower computers
}
if(alpha > 1) {  // better check for > 1 because == 1 will nearly always fail 
	alpha = 1;
}

Try to copy-paste your code next time, and use code tags ([ code ] … [ / code ]). It makes your code easier to read and helps us find the cause of the problem.

Good luck!