1

我正在开发一个包含精灵表的游戏,如下所示:

精灵表的 EG

我知道当你这样做时,spriteBatch.Draw(...)你可以绘制图像的某个部分,但对于我正在做的事情,我需要Texture2D每帧有一个单独的对象。

我已经完成了谷歌搜索,但我只能找到过时的代码:/

更新MJP在这里发布的代码几乎是我需要的......但是,RenderTarget2D.GetTexture()XNA 4.0 中没有函数。

4

1 回答 1

5

哇,好吧......更多的谷歌搜索显示:

Texture2D tex = (Texture2D)renderTarget;

只是一个简单的演员:)

这是我的最终代码:

    public static Texture2D Crop(Texture2D image, Rectangle source)
    {
        var graphics = image.GraphicsDevice;
        var ret = new RenderTarget2D(graphics, source.Width, source.Height);
        var sb = new SpriteBatch(graphics);

        graphics.SetRenderTarget(ret); // draw to image
        graphics.Clear(new Color(0, 0, 0, 0));

        sb.Begin();
        sb.Draw(image, Vector2.Zero, source, Color.White);
        sb.End();

        graphics.SetRenderTarget(null); // set back to main window

        return (Texture2D)ret;
    }
于 2011-11-30T19:36:35.207 回答