Search code examples
javaandroidlibgdx

LibGDX stage fading to black color


I have multiple screens with stages in my game and I have been implementing a simple fade transition between them.

It works now, but I want that the screen fades out to black. The clear color of the game is white, though, so only the stage is obviously faded out, and the background keeps white. I have tried adding Actions.color(Color.BLACK, Interpolation.fade) to the stage but nothing really happens at all.

How could I implement the fade transition correctly?


Solution

  • Stage doesn't have drawable content only a group that contains all children.

    So create full Screen black Image and add to stage as background. Then inside show() that make it white by action, as your required background.

    @Override
    public void show() {
    
        final Image image=new Image(new TextureRegion(GdxTest.getTexture()));
        image.setSize(stage.getWidth(),stage.getHeight());
        image.setOrigin(stage.getWidth()/2,stage.getHeight()/2);
        image.setColor(Color.BLACK);
    
        stage.addActor(image);
        stage.addListener(new ClickListener(){
    
            @Override
            public void clicked(InputEvent event, float x, float y) {
    
                image.addAction(Actions.sequence(Actions.color(Color.BLACK,2),Actions.run(new Runnable() {
                    @Override
                    public void run() {
                        ((GdxTest)Gdx.app.getApplicationListener()).setScreen(new SecondScreen());
                    }
                })));
    
                super.clicked(event, x, y);
            }
        });
    
        image.addAction(Actions.color(Color.WHITE,2)); 
    }
    

    getTexture() method

    public static Texture getTexture(){
    
        Pixmap pixmap;
        try {
            pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        }catch (GdxRuntimeException e)
        {
            pixmap=new Pixmap(1,1, Pixmap.Format.RGB565);
        }
        pixmap.setColor(Color.WHITE);
        pixmap.drawRectangle(0,0,1,1);
    
        return new Texture(pixmap);
    }