I am fairly new to xna. I just created a sprite with transparent background(magenta). Problem is my Rectangle is reading the coordinates of whole sprite not of visible one. How do I make it read only the visible sprite.
myrectangle = new Rectangle(0, 0, box.Width, box.Height);
I want to place my visible part not transparent at that position. Thanks in advance.
To transform a color to transparent, go to the texture properties, content processor, and enable Color Key, and set the key Color to magenta.

Then to positioning the sprite where you want, you need to set the proper origin.
To set the ship center in the desired position, is needed to set the origin as shown:

So when you draw it, you need doing similar to this:
var origin = new Vector2(40,40);
spritebatch.Draw(shipTexture, shipPosition, null, Color, origin, ...)
You can change your texture rectangle source too:
var texSource = new Rectangle( 25,25, 30,30);
spritebatch.Draw(shipTexture, shipPosition, texSource, Color)

Although you may need to change the origin if you want to position the ship at its center
You need to manually measure the offset of the point you need using a program like Paint and then set that offset in the parameter Origin in the Draw method.
A better idea is to measure the size in pixel of your sprite (without the background) and the set it as the sourceRectangle in the Draw method.
spritebatch.Draw(textureToDraw, Position, sourceRectangle, Color.White)
SourceRectangle is nullable, its defalut value is null, and in that case XNA will draw the whole texture, and you don't need that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With