首页 > 解决方案 > 单人游戏。各种绘图功能中的水平深度?

问题描述

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

我希望地板和房子比精灵低,这样它们就不会遮挡精灵。但我不知道如何为纹理分配深度级别。

标签: c#xnadrawmonogame

解决方案


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 值允许您执行诸如水平或垂直翻转纹理之类的操作。

推荐阅读