1
\$\begingroup\$

i use a stage on my background but the fadein doens't work. This is my code:

public class Level1 implements Screen{
                
//Background stage
Texture BackgroundLevel1;
private Stage BackgroundL1;    

NameGame game;

public Level1 (NameGame game){
    
    this.game = game;
    
    BackgroundL1 = new Stage();
    
    BackgroundLevel1 = new Texture("BackgroundLevel1.png");
    Image background = new Image(BackgroundLevel1);
    BackgroundL1.addActor(background);       
}

@Override
public void show() {
}

@Override
public void render(float f) {
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();

    BackgroundL1.addAction(Actions.fadeIn(1));
    game.batch.draw(BackgroundLevel1, 0, 0, NameGame.WIDTH, NameGame.HEIGHT);
    
game.batch.end();
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Do not call addAction every time in the render method, doing that will cause your Stage to register a new fadeIn action for every frame.

Also, make sure that the alpha of your Stage is zero when you start, otherwise there is nothin to fade in as the Stage is already fully visible on the first render.

Further, a Stage is rendered by calling draw on the Stage not by passing the Texture to a SpriteBatch, and in order for Actions to work, you need to call act on the Stage every frame so that is knows how much to advance the action.

Something closer to this might work for you:

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class SomeGameName extends Game {
    private Texture backgroundTexture;
    private Stage background;

    @Override
    public void create () {
        backgroundTexture = new Texture("badlogic.jpg");
        background = new Stage();
        Image image = new Image(backgroundTexture);
        background.addActor(image);
        background.addAction(
                // Add a sequence of actions, the first one immediately sets the alpha to zero, the second one fades in over 2.0 seconds
                Actions.sequence(
                        Actions.alpha(0.0f),
                        Actions.fadeIn(2.0f)
                )
        );
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        background.act(); // This makes the actions work
        background.draw(); // This renders the Stage
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ I am learning libgdx now and this explanation has been very helpful to me! It works, thanks! \$\endgroup\$
    – witsel
    Commented May 13, 2022 at 11:05
  • \$\begingroup\$ Cool, I am glad it worked for you @FrancoGioia, consider marking the question as answered if it solved your problem. \$\endgroup\$
    – bornander
    Commented May 13, 2022 at 12:12
  • \$\begingroup\$ @FrancoGioia to do so, click the checkmark icon in the top-left of the answer, just under the voting buttons. This marks the answer as "Accepted", to help future users more quickly find correct / working / helpful answers. \$\endgroup\$
    – DMGregory
    Commented May 13, 2022 at 12:15
  • \$\begingroup\$ Done! Thank you \$\endgroup\$
    – witsel
    Commented May 13, 2022 at 12:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .