For my game I want to make a map of the world. Per country I made a file with the border of the country and the rest of background is transparent. This way all my files have the same width and height. My problem is that XNA only renders the first image and it does it in an alphabetical order. For example: if I change the name of the first sprite so it isn't first in alphabetical order, it just renders the first sprite.

The sprites are loaded in a sortedlist and each key is the name of the continent + the name of the country. And I loop through this list to render each sprite.

Code to load the files:

protected override void LoadContent()
        DirectoryInfo objDirectory = new DirectoryInfo(Game.Content.RootDirectory + "/Regions");
        FileInfo[] objFiles = objDirectory.GetFiles("*.*");

        objSpritebatch = new SpriteBatch(Game.GraphicsDevice);

        foreach (FileInfo File in objFiles)
            string[] FileName = File.Name.Split("_".ToCharArray());
            string strContinent = FileName[1] + "/" + FileName[2].Substring(0, FileName[2].Length - 4);
            lstBorders.Add(strContinent, Game.Content.Load<Texture2D>("Regions\\" + Path.GetFileNameWithoutExtension(objFiles[0].Name)));


Code to render the files:

public override void Draw(GameTime gameTime)
        objSpritebatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);

        foreach(KeyValuePair<string, Texture2D> Border in lstBorders)
            objSpritebatch.Draw(Border.Value, new Rectangle(0, 0, 800, 400), Color.White);



I allready did some research and hoped it would be fixed by using those parameters in the begin command but it didn't help. I hope I gave you all the code you need.


1 Answer 1


You are drawing all sprites in the same postition and same size, if textures don't have alpha channel you will only see one of them.

Test this to draw a grid with 8 columns:

    var size = 100; 
    var width = 8;
    var n=0;
    foreach(KeyValuePair<string, Texture2D> Border in lstBorders)
        var x = (n % width)*size;
        var y = (n/width) * size;
        objSpritebatch.Draw(Border.Value, new Rectangle(x, y, size, size), Color.White);

If you can see all, them you have a problem with the alpha channel of your textures. Sometimes the alpha channel is stored as a key color. You can enable an option in the texture processor of the image to manage this situation. Going to Texture/Properties/ContentProcessor/Color Key Enabled

And related to order, if you use a SortedList to store sprites, it will be orderer by the key, your key is the name of the file, so it's ordered alphabetically by texture name, if you want other order, change your key or the file names.

You can use a file name format like "Continent\00-Europe.jpg" to give the desired order.

  • \$\begingroup\$ I drew the grid and it drew the same image. I had a grid with the same image. Is it possible there's something wrong in the loading? Edit: Thanks to this answer I found my problem: in the loading, I always load the same file, I should use my variable File from the loop instead of objFiles \$\endgroup\$
    – Velcio
    Commented Aug 31, 2013 at 10:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .