【问题标题】:How can I draw a rectangle or sprite onto another sprite in xna?如何在 xna 中将矩形或精灵绘制到另一个精灵上?
【发布时间】:2014-03-07 17:41:15
【问题描述】:

我正在 xna 中开发一款 2d 游戏。基本上我想在另一个有碰撞的精灵上绘制一个矩形或精灵。我可以检测到碰撞和一切,但我不知道如何在精灵上绘图。我在精灵上绘制矩形,但它在精灵后面绘制,这使得矩形不可见。有没有办法做到这一点?

【问题讨论】:

  • 它在精灵后面绘制,因为在您的绘制方法中,您首先绘制它。而是把它画第二个。 (在你的精灵被绘制之后)
  • 我按照你说的画法。这也是两个不同的精灵批次,有效果吗?
  • 因为我有多个类,所以无法显示,基本上它们是嵌套的精灵批次。
  • 向我们展示与您的问题相对应的代码并注释掉其余部分。
  • 没关系,我明白了。我刚刚从 main 访问 spritebatch 实例并调用它。不过还是谢谢。

标签: sprite xna-4.0 collision


【解决方案1】:

这是一个层排序的例子:

    public enum TextureName : byte
    {
        Black,
        Yellow
    }
    private GraphicsDeviceManager graphics;
    private SpriteBatch spriteBatch;
    public struct Sprite
    {
        public Vector2 Position;
        public Texture2D Texture;
        public Sprite(Texture2D texture, Vector2 position)
        {
            Position = position;
            Texture = texture;
        }
    }

    public Dictionary<TextureName, Sprite> Sprites { get; private set; }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Sprites = new Dictionary<TextureName, Sprite>();
        Vector2 position = new Vector2(graphics.PreferredBackBufferWidth / 2 - 64, graphics.PreferredBackBufferHeight / 2 - 64);
        Sprites.Add(TextureName.Black, new Sprite(Content.Load<Texture2D>(@"black_sprite"), position));
        Sprites.Add(TextureName.Yellow, new Sprite(Content.Load<Texture2D>(@"yellow_sprite"), position + new Vector2(32, 32)));
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
        spriteBatch.Draw(Sprites[TextureName.Black].Texture, Sprites[TextureName.Black].Position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);// -> will be drawn first
        spriteBatch.Draw(Sprites[TextureName.Yellow].Texture, Sprites[TextureName.Yellow].Position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);// -> will be drawn second
        spriteBatch.End();
        base.Draw(gameTime);
    }

所以黑色精灵会在后面。

您可以使用 SpriteBatch.Begin() 方法的 SpriteSortMode 并为 SpriteBatch.Draw() 方法设置图层深度,如下所示,轻松地重新排序您的 Sprite。

protected override void Draw(GameTime gameTime)
{
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
        spriteBatch.Draw(Sprites[TextureName.Black].Texture, Sprites[TextureName.Black].Position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f/* layer depth*/);
        spriteBatch.Draw(Sprites[TextureName.Yellow].Texture, Sprites[TextureName.Yellow].Position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f/* layer depth*/);
        spriteBatch.End();
        base.Draw(gameTime);
}

输出将是:

如果您使用与其他类不同的精灵批次。第一个例子可以正常工作。只需注意将“YourClass.Draw()”按正确顺序放入主游戏绘制方法中即可。

注意使用第二个示例。它的工作速度比第一个慢得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多