[solved] [libGdx] Why sprite.setColor() doesn't work ?

Hello everyone ;D !!!

i have a quick question about tinting a sprite, so i have a white circle PNG image, which i want to draw it in different colors.
In the Wiki, the closest and most “legit” thing to use is sprite.setColor(theNewColor), however this doesn’t work, so i found myself forced to use spriteBatch.setColor(theNewColor) then go back to the default color after finishing drawing the object.

My question is, why the first approach doesn’t work ? (i tried it in the Create method and also in the Render method)

thank you

(very happy to ask here again ;D )

should work !
Just tried it since I rarely use the Sprite class.

show some code I guess.

this doesn’t work :


package com.aladine.rgbx;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class RgbX extends ApplicationAdapter {
	SpriteBatch batch;
	Texture circleImg;
	Sprite center, redPad, bluePad, greenPad, yellowPad;
	Color whiteCol, redCol, greenCol, blueCol, yellowCol;

	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 480;
	public static Vector2 screenCenter;

	private Group group;
	private Stage stage;

	@Override
	public void create() {
		batch = new SpriteBatch();
		circleImg = new Texture("circle.png");
		screenCenter = new Vector2(WIDTH / 2, HEIGHT / 2);

		center = new Sprite(circleImg);
		center.setPosition(screenCenter.x - center.getWidth() / 2,
				screenCenter.y - center.getWidth() / 2);

		// create the colors
		setColors();
		// change the sprite color to blue
		center.setColor(blueCol);

	}

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

		batch.begin();
		batch.draw(center, center.getX(), center.getY(), center.getOriginX(),
				center.getOriginY(), center.getWidth(), center.getHeight(), 1,
				1, center.getRotation());
		batch.end();

	}

	private void setColors() {
		redCol = new Color(0.9f, 0.42f, 0.42f, 1);
		blueCol = new Color(0.42f, 0.42f, 0.9f, 1);
		greenCol = new Color(0.42f, 0.9f, 0.9f, 1);
		yellowCol = new Color(0.98f, 0.99f, 0.42f, 1);
		whiteCol = new Color(1, 1, 1, 1);
	}
}

this work :

package com.aladine.rgbx;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class RgbX extends ApplicationAdapter {
	SpriteBatch batch;
	Texture circleImg;
	Sprite center, redPad, bluePad, greenPad, yellowPad;
	Color whiteCol, redCol, greenCol, blueCol, yellowCol;

	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 480;
	public static Vector2 screenCenter;

	private Group group;
	private Stage stage;

	@Override
	public void create() {
		batch = new SpriteBatch();
		circleImg = new Texture("circle.png");
		screenCenter = new Vector2(WIDTH / 2, HEIGHT / 2);

		center = new Sprite(circleImg);
		center.setPosition(screenCenter.x - center.getWidth() / 2,
				screenCenter.y - center.getWidth() / 2);

		// create the colors
		setColors();
		

	}

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

		batch.begin();
		batch.setColor(blueCol);
		batch.draw(center, center.getX(), center.getY(), center.getOriginX(),
				center.getOriginY(), center.getWidth(), center.getHeight(), 1,
				1, center.getRotation());
		batch.end();

	}

	private void setColors() {
		redCol = new Color(0.9f, 0.42f, 0.42f, 1);
		blueCol = new Color(0.42f, 0.42f, 0.9f, 1);
		greenCol = new Color(0.42f, 0.9f, 0.9f, 1);
		yellowCol = new Color(0.98f, 0.99f, 0.42f, 1);
		whiteCol = new Color(1, 1, 1, 1);
	}
}

thank you

use Sprite method to draw it not SpriteBatch method. You are calling draw function that takes textureRegion as parameter and Sprite extent TextureRegion so its valid code but does not work like you are trying to use it.

Thank you this solved the problem ;D