c# - 单人游戏。各种绘图功能中的水平深度?
问题描述
在 animatedSprite.cs 我有:
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Columns;
int height = Texture.Height / Rows;
int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, 120, 140);
if (Game1.przelacznik_w_bezruchu == true) { sourceRectangle = new Rectangle(0, 0, width, height); }
spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
spriteBatch.End();}
在 GameScene.cs 中
private void DrawGame(){
spriteBatch.Begin();
spriteBatch.Draw(floor1, rec_floor1, color1);
spriteBatch.Draw(floor2, rec_floor2, color1);
spriteBatch.Draw(house1, rec_house1,color1);
spriteBatch.End();}
我希望地板和房子比精灵低,这样它们就不会遮挡精灵。但我不知道如何为纹理分配深度级别。
解决方案
SpriteBatch Draw 函数有一个重载,允许您指定 layerDepth。
您将在动画Sprite.cs 的 Draw 调用中使用以下内容:
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, layerDepth);
layerDepth 将是一个浮点值,它允许您控制精灵的排序顺序。这可以从您的 GameScene.cs 作为浮点参数传入,或者您可以将位置参数更改为 Vector3 并使用其 Z 变量。
至于其他参数:
- 0.0f 是精灵的旋转值(在 Z 轴上)
- Vector2.Zero 是“原点”参数,它允许您控制精灵的旋转点。这对于围绕左上角以外的点(通常是中心)旋转很有用
- SpriteEffects.None 这使您的绘图调用与原始帖子中的相同。不同的 SpriteEffect 值允许您执行诸如水平或垂直翻转纹理之类的操作。
推荐阅读
- python - 将 Twitter 引擎搜索转换为 pandas 数据框
- jestjs - 使用 Angular ESLint 进行 Spy 和 callThrough
- database - 是否可以创建一个安全连接到用户指定数据库的 web 应用程序?
- html - 这个输入类型=日期默认值我做错了什么?模板驱动形式 Angular
- javascript - 反应应用程序添加大文本块的最佳方法是什么
- java - 如何防止 RSocket 在 Spring Boot 中允许跨源?
- python - 用于在 Python 中检测公司名称的正则表达式
- ios - 在 UndoManager 中支持不重做的撤消
- r - R中的求和更快的方法
- reactjs - React - 状态重置为 UseEffect 挂钩内的空数组