user2375782
user2375782

Reputation: 65

Xna draw order not working right

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

Upvotes: 0

Views: 1447

Answers (2)

Dmi7ry
Dmi7ry

Reputation: 1777

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

Upvotes: 0

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

Upvotes: 2

Related Questions