Search code examples
javaandroidlibgdx

How to make a "hand-made" actor fade LibGDX


I am trying to create a black rectangle that fades in when a certain action is done in my application, but I can't seem to find a way to do it. I created a new Rectangle class that extends Scene2D's Actor and this is how it looks:

public class Rectangle extends Actor{
   private Texture texture;

   public Rectangle(float x, float y, float width, float height, Color color) {
      createTexture((int)width, (int)height, color);

      setX(x);
      setY(y);
      setWidth(width);
      setHeight(height);
  }

  private void createTexture(int width, int height, Color color) {
      Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
      pixmap.setColor(color);
      pixmap.fillRectangle(0, 0, width, height);
      texture = new Texture(pixmap);
      pixmap.dispose();
  }

  @Override
  public void draw(Batch batch, float parentAlpha) {
      Color color = getColor();
      batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
      batch.draw(texture, getX(), getY(), getWidth(), getHeight());
  }
}

The problem is when I draw the rectangle, I have to set a constant parentAlpha. If I call:

rectangle.addAction(alpha(0.5f));

It won't do anything, because the alpha will not change. Is there a way to do it without having a fixed parentAlpha?


Solution

  • What do mean by fixed parentAlpa ?

    From your question, You want to fade in a black Rectangle. To achieve this set alpha value of your actor to zero and add fadeIn() action when you want to appear on Screen.

    public class MultiLine extends ApplicationAdapter {
    
      Stage stage;
    
      @Override
      public void create() {
    
        stage=new Stage();
    
        RectActor rectActor=new RectActor(100,100,100,100, Color.BLACK);
        RectActor rectActor1=new RectActor(150,150,100,100, Color.BLACK);
    
        stage.addActor(rectActor1);
        stage.addActor(rectActor);
    
        rectActor.getColor().a=0;                //Make transparent
        rectActor.addAction(Actions.fadeIn(1f)); // In one second rectActor appear on Screen.
      }
    
      @Override
      public void render() {
    
        Gdx.gl.glClearColor(1,0,0,0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
        stage.draw();
        stage.act();
      }
    
      @Override
      public void dispose() {
          stage.dispose();
      }
    }
    

    Rectangle is already in API so it's better to use different name.

    public class RectActor extends Actor {
    
        private Texture texture;
    
        public RectActor(float x, float y, float width, float height, Color color) {
            createTexture((int)width, (int)height, color);
    
            setX(x);
            setY(y);
            setWidth(width);
            setHeight(height);
        }
    
        private void createTexture(int width, int height, Color color) {
            Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
            pixmap.setColor(color);
            pixmap.fillRectangle(0, 0, width, height);
            texture = new Texture(pixmap);  //creating texture for each RectActor is not profitable so create once and share in all RectActor
            pixmap.dispose();
        }
    
        @Override
        public void draw(Batch batch, float parentAlpha) {
            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            batch.draw(texture, getX(), getY(), getWidth(), getHeight());
        }
    }