我是xna的新手。我刚刚创建了一个透明背景的精灵(品红色)。问题是我的矩形读取的是整个精灵的坐标,而不是可见的坐标。我如何让它只读可见的精灵。
myrectangle = new Rectangle(0, 0, box.Width, box.Height);我想把我的可见部分放在那个位置不透明。提前谢谢。
发布于 2013-08-24 19:01:15
要将颜色转换为透明,请转到纹理属性、内容处理器并启用颜色键,然后将键颜色设置为洋红色。

然后,为了将sprite定位在您想要的位置,您需要设置正确的原点。
要将船舶中心设置在所需位置,需要设置原点,如下所示:

所以当你绘制它的时候,你需要做类似的事情:
var origin = new Vector2(40,40);
spritebatch.Draw(shipTexture, shipPosition, null, Color, origin, ...)你也可以改变你的纹理矩形源:
var texSource = new Rectangle( 25,25, 30,30);
spritebatch.Draw(shipTexture, shipPosition, texSource, Color)

但如果要将船定位在其中心,则可能需要更改原点
发布于 2013-08-24 18:52:35
您需要使用像Paint这样的程序手动测量所需点的偏移,然后在Draw方法的参数Origin中设置该偏移。
一个更好的想法是以像素为单位测量精灵(没有背景)的大小,并在Draw方法中将其设置为sourceRectangle。
spritebatch.Draw(textureToDraw, Position, sourceRectangle, Color.White)SourceRectangle是可以为空的,它的默认值是null,在这种情况下,XNA将绘制整个纹理,您不需要这样做。
发布于 2013-08-28 14:32:12
使用像Magenta这样的透明颜色编码是非常过时的。如今,我们使用图像中的alpha来实现这一点。
我想要做的唯一真正的方法是搜索颜色数据,找到α> 0的最小和最大的x和y坐标,在您的例子中是!= Color.Magenta。
Texture2D sprite = Content.Load<Texture2D>(.....);
int width = sprite.Width;
int height = sprite.Height;
Rectangle sourceRectangle = new Rectangle(int.Max, int.Max, 0, 0);
Color[] data = new Color[width*height];
sprite.GetData<Color>(data);
int maxX = 0;
int maxY = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = width * y + x;
if (data[index] != Color.Magenta)
{
if (x < sourceRectangle.X)
sourceRectangle.X = x;
else if (x > maxX)
maxX = x;
if (y < sourceRectangle.Y)
sourceRectangle.Y = y;
else if (y > maxY)
maxY = y;
}
}
}
sourceRectangle.Width = maxX - sourceRectangle.X;
sourceRectangle.Height = maxY - sourceRectange.Y;https://stackoverflow.com/questions/18416924
复制相似问题