Search code examples
javalibgdxtextures

Can I tint texture inside an actor?


I need to tint an item in my ShopCell (it should be black at first, and after purchase it becomes normal).

public class ShopCell extends Group {
private final float WIDTH = 100;
private final float HEIGHT = 150;
private Label label;

public ShopCell(TextureRegion itemTxt, int price) {
    this.setSize(WIDTH, HEIGHT);
    Image cellBg = new Image(Resource.cellBg);
    cellBg.setSize(WIDTH, HEIGHT);
    Image item = new Image(itemTxt);
    float aspectRatio = item.getWidth()/item.getHeight();
    item.setBounds(cellBg.getX() + WIDTH/2-(HEIGHT*0.3f*aspectRatio), cellBg.getY() + HEIGHT/3, HEIGHT*0.6f*aspectRatio, HEIGHT*0.6f);
    label = new Label(String.valueOf(price), new Label.LabelStyle(Resource.font, Color.YELLOW));
    label.setPosition(cellBg.getX() + WIDTH/2 - label.getWidth()/2, cellBg.getY() + HEIGHT/15);
    this.addActor(cellBg);
    this.addActor(item);
    this.addActor(label);
}

I've tried to make Sprite at first, tint it like this and add it to Image:

Sprite sprite = new Sprite(itemTxt);
sprite.setColor(Color.BLACK);
Image item = new Image(sprite);

But when I do so, my texture doesn't tint. How can I tint it and add it to the Group? And can I return its normal appearence then (for purchased items)?


Solution

  • Sprite class having own color so that can be tint but when you create Image from that Sprite, only TextureRegion of Sprite used for construction of new Image object.

    Set color of Image instead of tinted Sprite :

    Image item = new Image(itemTxt);
    item.setColor(Color.BLACK);
    

    You can also try fadeIn/Out Action on your object

    image.addAction(Actions.fadeOut(2));