Just curious if anyone could help out a beginner here. Is there a way to change the opacity of a sprite or texture when drawing in 2D I'd like to be able to make some of my sprites semi-transparent.
Falklian, I don't have XNA GSE in front of me, so keep that in mind.
My guess would be to build your own Color structure to pass to the Draw method in SpriteBatch rather than Color.White, since the Color constructor has an overload that accepts a Byte value for the Alpha component. So, for your SpriteBatch, you might do something like the following (to draw a half-transparent sprite):
Color myTransparentColor = new Color(255, 255, 255, 127);
Like I said, I can't verify this, but it's what I would try first.
For an example based Color.White, check out the XNA documentation How To (XNA -> XNA GSE -> Programming Guide -> Graphics -> 2D Graphics -> How To: Draw a Sprite). Just modify that to use the transparent color above.
I hope this helps (since I can't check myself) :).
Okay, I got home and tried this out. A quick note: if you want the alpha components to actually be transparent, you need to add SpriteBlendMode.AlphaBlend in your call to mySpriteBatch.Begin(). So, it might look like the following:
mySpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
mySpriteBatch.Draw(myTexture, myDestination, new Color(255, 255, 255, 127));
mySpriteBatch.End();
Without adding the SpriteBlendMode.AlphaBlend parameter to the Begin() call, your textures will not be alpha blended.
How do I change the opacity of a sprite?
spoimala
Yes, that's how you do it.
CPPUSer7
Falklian, I don't have XNA GSE in front of me, so keep that in mind.
My guess would be to build your own Color structure to pass to the Draw method in SpriteBatch rather than Color.White, since the Color constructor has an overload that accepts a Byte value for the Alpha component. So, for your SpriteBatch, you might do something like the following (to draw a half-transparent sprite):
Color myTransparentColor = new Color(255, 255, 255, 127);
mySpriteBatch.Draw( myTexture, myDestination, myTransparentColor);
Like I said, I can't verify this, but it's what I would try first.
For an example based Color.White, check out the XNA documentation How To (XNA -> XNA GSE -> Programming Guide -> Graphics -> 2D Graphics -> How To: Draw a Sprite). Just modify that to use the transparent color above.
I hope this helps (since I can't check myself) :).
IGiberson
Okay, I got home and tried this out. A quick note: if you want the alpha components to actually be transparent, you need to add SpriteBlendMode.AlphaBlend in your call to mySpriteBatch.Begin(). So, it might look like the following:
mySpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
mySpriteBatch.Draw(myTexture, myDestination, new Color(255, 255, 255, 127));
mySpriteBatch.End();
Without adding the SpriteBlendMode.AlphaBlend parameter to the Begin() call, your textures will not be alpha blended.
shakalama
Thanks for the replies!
Bill