Xna draw order not working right

大兔子大兔子 提交于 2019-12-20 05:52:44

问题


I have an 2d array of Texture2D, it holds the different parts of the map in this array. 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. 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.

Here is my draw method for my map which I call in the Game's Draw method:

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()

回答1:


Try inverting the order that they're drawn, AND use SpriteSortMode.Deferred




回答2:


You can try use overloaded SpriteBatch.Draw method with depth. 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.

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)

P.S. Sorry for google-translating

oops... I have not updated comments before responding



来源:https://stackoverflow.com/questions/16618343/xna-draw-order-not-working-right

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!