2D Sprites 的叠加问题 (XNA)

发布于 2024-10-09 16:48:44 字数 587 浏览 0 评论 0原文

这可能是一个菜鸟问题,但事实是...

我正在使用 Visual Studio 和 XNA Framework (3.1),我只会为我的游戏绘制 2D 精灵,这是主类中的 Draw 方法:

GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            background.Draw(this.spriteBatch);
            player1.Draw(this.spriteBatch);
            player2.Draw(this.spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);

问题是玩家2与玩家1重叠(因为他是在之后绘制的),并且我希望最后绘制两个玩家中最低的一个(按他们的位置)(以模拟深度)。

提前致谢! (对不起我的英语,我是法国人......)

It may be a noob question but here it is...

I'm using Visual Studio with the XNA Framework (3.1) and I'm only going to draw 2D sprites for my game, here is the Draw Method in the main class:

GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            background.Draw(this.spriteBatch);
            player1.Draw(this.spriteBatch);
            player2.Draw(this.spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);

The problem is that the player2 is overlapping player1 (because he is drawn after), and I'd like the lowest one of the 2 players (by their position) to be drawn last (to simulate depth).

Thanks in advance! (and sorry for my Engrish, I'm French...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-10-16 16:48:44

SpriteBatch.Draw 方法有一个重载,它提供了一个“layerDepth”参数(从 0 表示前面,到 1 表示后面)。您可以设置单个精灵的layerDepth并在SpriteBatch.Begin方法中使用spriteSortMode.BackToFront。

There is an overload of the SpriteBatch.Draw method which offers a 'layerDepth' parameter (from 0 for front, to 1 for back). You can set an individual sprite's layerDepth and use spriteSortMode.BackToFront in the SpriteBatch.Begin method.

爱,才寂寞 2024-10-16 16:48:44

好的,谢谢史蒂夫,我做到了。
我刚刚将播放器 SpriteBatch.Draw 方法更改为 SpriteBatch.Draw(X,X,(...),layerDepth)
层深度随着玩家的 Y 位置而更新。
必须使用 spriteSortMode.FrontToBack:由于原点位于左上角,所以一切都颠倒了。
谢谢!

Ok thanks to Steve I did it.
I just changed my player SpriteBatch.Draw method to SpriteBatch.Draw(X,X,(...),layerDepth)
with layerDepth getting updated with the Y position of the players.
Had to use spriteSortMode.FrontToBack: everything was reversed because of the origin being in the upper-left corner.
Thanks!

你与清晨阳光 2024-10-16 16:48:44

史蒂夫的答案可能是最简单的,但有时立即绘图是有利的。
另一种方法是根据 Y 轴对列表中的玩家和对象进行排序。

Steve's Answer is probably easiest , but sometimes Immediate drawing is favorable.
An alternative is sort your players and objects in a list based on their Y axis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文