Draw Rectangle with XNA

后端 未结 3 2004
误落风尘
误落风尘 2020-12-14 01:14

I am working on game. I want to highlight a spot on the screen when something happens.

I created a class to do this for me, and found a bit of code to draw the rect

3条回答
  •  有刺的猬
    2020-12-14 01:53

    The SafeArea demo on the XNA Creators Club site has code to do specifically that.

    You don't have to create the Texture every frame, just in LoadContent. A very stripped down version of the code from that demo:

    public class RectangleOverlay : DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Texture2D dummyTexture;
        Rectangle dummyRectangle;
        Color Colori;
    
        public RectangleOverlay(Rectangle rect, Color colori, Game game)
            : base(game)
        {
            // Choose a high number, so we will draw on top of other components.
            DrawOrder = 1000;
            dummyRectangle = rect;
            Colori = colori;
        }
    
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });
        }
    
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(dummyTexture, dummyRectangle, Colori);
            spriteBatch.End();
        }
    }
    

提交回复
热议问题