仅重绘一个纹理,而不是全部纹理

时间:2014-08-12 11:30:18

标签: c# xna monogame

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

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);
    }

1 个答案:

答案 0 :(得分:1)

尝试将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);
}