如果你想在你现有的纹理上绘制矩形,这就完美了。伟大的,当你想测试/查看碰撞
http://bluelinegamestudios.com/blog/posts/drawing-a-hollow-rectangle-border-in-xna-4-0/
-----从网站-----
的基本技巧,绘制形状是使单个像素纹理是白色,然后您可以与其他颜色混合并以坚固的形状进行显示。
// At the top of your class:
Texture2D pixel;
// Somewhere in your LoadContent() method:
pixel = new Texture2D(GameBase.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
pixel.SetData(new[] { Color.White }); // so that we can draw whatever color we want on top of it
在draw()方法
然后做这样的事情:
spriteBatch.Begin();
// Create any rectangle you want. Here we'll use the TitleSafeArea for fun.
Rectangle titleSafeRectangle = GraphicsDevice.Viewport.TitleSafeArea;
// Call our method (also defined in this blog-post)
DrawBorder(titleSafeRectangle, 5, Color.Red);
spriteBatch.End();
这确实绘图的实际方法:
private void DrawBorder(Rectangle rectangleToDraw, int thicknessOfBorder, Color borderColor)
{
// Draw top line
spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, rectangleToDraw.Width, thicknessOfBorder), borderColor);
// Draw left line
spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, thicknessOfBorder, rectangleToDraw.Height), borderColor);
// Draw right line
spriteBatch.Draw(pixel, new Rectangle((rectangleToDraw.X + rectangleToDraw.Width - thicknessOfBorder),
rectangleToDraw.Y,
thicknessOfBorder,
rectangleToDraw.Height), borderColor);
// Draw bottom line
spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X,
rectangleToDraw.Y + rectangleToDraw.Height - thicknessOfBorder,
rectangleToDraw.Width,
thicknessOfBorder), borderColor);
}
+1伟大的答案。正是我正在寻找的!谢谢 – 2010-08-12 00:47:39
+1短而正是我所需要的。 – FRoZeN 2011-08-02 15:30:45