只重画一个纹理,而不是全部

本文关键字:纹理 全部 一个 | 更新日期: 2023-09-27 18:17:32

我是XNA的新手,我遇到了一个问题。我有一个叫做"dot"的纹理,我用它在屏幕上画一条线。然后我有一个纹理,我称之为"工具",当我用手指触摸它时,我想移动它。我遇到的问题是,对于要清除和重新绘制的工具,我调用graphicsDevice。清除XNA中的方法。但是当我这样做的时候,我的线条也消失了。所以我的问题是,我如何移动工具,而不会导致我的线条消失?有没有一种方法可以只重新绘制工具,而不重新绘制线条?下面是我的代码片段,第一个update-method:

protected override void Update (GameTime gameTime)
    {
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        TouchCollection touchCollection = TouchPanel.GetState ();
        foreach (TouchLocation tl in touchCollection) {
            if ((tl.State == TouchLocationState.Pressed) || (tl.State == TouchLocationState.Moved)) {
                toolPos = touchCollection [0].Position;
            }
        }
        base.Update (gameTime);
    }

和绘制方法:

        protected override void Draw (GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear (Color.Black);
        newVector = nextVector (oldVector);
        spriteBatch.Begin ();
        DrawEvenlySpacedSprites (dot, oldVector, newVector, 0.9f);
        spriteBatch.Draw (tool, toolPos, Color.White);
        spriteBatch.End ();
        oldVector = new Vector2 (newVector.X, newVector.Y);
        base.Draw (gameTime);
    }

只重画一个纹理,而不是全部

尝试使用RenderTarget2DRenderTargetUsage.PreserveContents。如果你还没有使用渲染目标,这里有一个教程:http://rbwhitaker.wikidot.com/render-to-texture

你所要做的就是在目标的构造函数中指定RenderTargetUsage.PreserveContents,在上面画点,然后把目标和工具画到屏幕上。它应该看起来像这样:

protected override void Draw (GameTime gameTime)
{
    graphics.GraphicsDevice.Clear (Color.Black);
    newVector = nextVector (oldVector);
    GraphicsDevice.SetRenderTarget(renderTarget);
    spriteBatch.Begin ();
    DrawEvenlySpacedSprites (dot, oldVector, newVector, 0.9f);
    spriteBatch.End ();
    GraphicsDevice.SetRenderTarget(null);
    spriteBatch.Begin();
    spriteBatch.Draw(renderTarget, new Vector2(), Color.White);
    spriteBatch.Draw (tool, toolPos, Color.White);
    spriteBatch.End()
    oldVector = new Vector2 (newVector.X, newVector.Y);
    base.Draw (gameTime);
}