6
\$\begingroup\$

I have this

batch.draw(currentFrame, x, y, this.parent.originX, this.parent.originY, this.parent.width, this.parent.height, this.scaleX, this.scaleY,this.rotation);

I want to apply the alpha that it gets from the method, but theres is not overload from the SpriteBatch class that takes the alpha value, is there some wey to apply it? (i did it this way, because this are animation, and i wanted to control them) in my static ones i apply

sprite.draw(SpriteBatch, alpha)

Thanks

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

There isn't a built-in way of doing this, but you can do it by temporarily modifying the Color which SpriteBatch blends your texture with; usually used for tinting though. This is untested, but it should work...Let me know if it does.

Color color = batch.getColor();//get current Color, you can't modify directly
float oldAlpha = color.a; //save its alpha

//From here you can modify alpha however you want
float scale = 0.5;
color.a = oldAlpha*scale; //ex. scale = 0.5 will make alpha halved
batch.setColor(color); //set it
batch.draw(currentFrame, x, y, this.parent.originX, this.parent.originY, this.parent.width, this.parent.height, this.scaleX, this.scaleY,this.rotation);

//Set it back to orginial alpha when you're done with your alpha manipulation
color.a = oldAlpha;
batch.setColor(color);

This is actually similar to what sprite.draw(SpriteBatch, alpha) do if you look at source.

\$\endgroup\$
2
  • \$\begingroup\$ To change the alpha of the batch when you use batch.draw, you need to call batch.setColor(color). If you use sprite.draw, then call setColor and sprite.draw(batch, alphaModulation). \$\endgroup\$
    – Yoo Matsuo
    Nov 26, 2015 at 12:28
  • \$\begingroup\$ @YooMatsuo, thanks for pointing it out! Edited answer. \$\endgroup\$ Nov 26, 2015 at 18:52
2
\$\begingroup\$

Another way could be to use Sprites instead of TextureRegion for the Animation (you have to cast the TextureRegion probably) since Sprite has color field which uses when using it with the SpriteBatch. However, if you change to use Sprites you need to know you have to call sprite.render(spriteBatch) and no the inverse way.

Btw, if the previous answer by XiaoChuan Yu was useful remember to mark your question as answered so it is removed from unanswered questions.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .