简体   繁体   English

XNA绘图顺序无法正常运行

[英]Xna draw order not working right

I have an 2d array of Texture2D, it holds the different parts of the map in this array. 我有一个Textured2D二维数组,它在该数组中保存地图的不同部分。 I have a problem though, when I run the game, the map is drawn correctly but for some reason the array[0, 0] texture overlaps all my textures including my player texture and mouse texture. 但是,我有一个问题,当我运行游戏时,可以正确绘制地图,但是由于某些原因,array [0,0]纹理与我的所有纹理(包括玩家纹理和鼠标纹理)重叠。 Every other texture works as my mouse and player texture correctly overlaps the map. 当我的鼠标和播放器纹理正确重叠在地图上时,其他所有纹理均起作用。

I'm really confused right now as the map textures are being drawn together using a nested for loop. 我现在真的很困惑,因为地图纹理是使用嵌套的for循环绘制在一起的。

Here is my draw method for my map which I call in the Game's Draw method: 这是我在游戏的Draw方法中调用的地图绘制方法:

public void Draw()
{
    // Draws the Map from a 2D Array
    for (int row = 0; row < mapTexture.GetLength(0); row++)
    {
        for (int col = 0; col < mapTexture.GetLength(1); col++)
        {
            spriteBatch.Draw(mapTexture[row, col], mapPosition[row, col], Color.White);
        }//end for
    }//end for
}//end Draw()

My actual draw method: 我的实际绘制方法:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
    spriteBatch.Draw(mouseIcon, mouseIconPosition, Color.White);
    player.Draw();
    map.Draw();
    spriteBatch.End();

    base.Draw(gameTime);
}//end Draw()

尝试颠倒它们的绘制顺序,并使用SpriteSortMode.Deferred

You can try use overloaded SpriteBatch.Draw method with depth . 您可以尝试使用带有depth的重载SpriteBatch.Draw方法。 As example: 例如:

SpriteBatch.Draw (Texture2D, Vector2, Nullable, Color, Single, Vector2, Single, SpriteEffects, Single) Adds a sprite to the batch of sprites to be rendered, specifying the texture, screen position, optional source rectangle, color tint, rotation, origin, scale, effects, and sort depth. SpriteBatch.Draw(Texture2D,Vector2,Nullable,Color,Single,Vector2,Single,SpriteEffects,Single)将Sprite添加到要渲染的Sprite批中,并指定纹理,屏幕位置,可选的源矩形,色彩,旋转,起源,规模,效果和分类深度。

or can try change order for drawing: 或者可以尝试更改图纸的顺序:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

    map.Draw();  // first
    player.Draw();  // second
    spriteBatch.Draw(mouseIcon, mouseIconPosition, Color.White); // third

    spriteBatch.End();

    base.Draw(gameTime);
}//end Draw()

(its for SpriteSortMode.Deferred) (适用于SpriteSortMode.Deferred)

PS Sorry for google-translating PS对不起,谷歌翻译

oops... I have not updated comments before responding 糟糕...回应之前我尚未更新评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM