SpriteBatch color (for tinting) affects the whole picture

I created an AnimatedSprite class that drew a specific TextureRegion . Sometimes I need a hue color effect, so I set (this.color is the Color field of my AnimatedSprite ):

 super.draw(batch, parentAlpha); batch.setColor(this.color); batch.draw(this.frames[this.currentFrame], x, y, originX, originY, width, height, scaleX, scaleY, rotation) batch.setColor(Color.WHITE); 

However, when I have an AnimatedSprite color set to black or any color, everything else has that hue. I even try flush() , end the batch and start a new one, etc., but nothing works.

Please help me apply the hue effect correctly. I would appreciate any idea.

+4
source share
2 answers

Beware of shared Color objects! If you do:

 this.color = Color.WHITE; 

And then mutate this.color later, you will mutate Color.WHITE , which is usually incorrect! :)

Always create a copy when creating a Color object that you will mutate:

 this.color = new Color(Color.WHITE); 

Many objects in libGDX change like this (whereas similar objects in the regular Java library will be unchanged), since libGDX (rightfully) is very concerned about GC crashes.

+6
source

Instead of using

 this.color = new Color(Color.WHITE); 

you can use:

 batch.setColor(Color.WHITE.tmp()); 

This will create a temporary copy of white and looks a little cleaner for me.

+1
source

All Articles