0
votes

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()
2
where are you drawing your mouse and player?Sam I am says Reinstate Monica
edit it into your questionSam I am says Reinstate Monica
are you sure that your mouse is on top of the rest of the map? judging from the code you've posted, it shouldn't beSam I am says Reinstate Monica
Yup, its ontop of the player, and the player is on top of the map. Well, except the top right peice of the map of course.user2375782
I have also tried doing FrontToBack but I see no difference.user2375782

2 Answers

2
votes

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

0
votes

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