Random textures in XNA
textures, xna
Solution
If I remember right, the Game.LoadContent() method is called only once while initializing (Game.Initialize()) to load the game's resources. You could force the game to reload those resources, but since you don't want to reload all of your resources, I would suggest to load all images you need in the LoadContent() method, like that:
List<Texture2D> texturePool = new List<Texture2D>();
Random rng = new Random();
protected override void LoadContent()
{
for(int i = 0; i < 4; i++)
texturePool.Add(Content.Load<Texture2D>("image" + i.ToString()));
}
And then before an enemy spawns you change the used texture by choosing one out of the loaded pool.
enemyTex = texturePool[rng.NextInt(texturePool.Count)];
And maybe you could alter the title to "Random textures in XNA" or something like that, since you want to change the texture on every spawn rather than every draw and this technique can be applied in many more situations.
Problem
Okay so I have this game I'm working on, I'm new to XNA (I'm using 4.0) and what I want to do is have a different texture every time an enemy is spawned. So I have these images "image1.png", "image2.png" and so on. I want it to use a different texture every time a new enemy is spawn, so far it only loads a random image when the game runs, so the problem must be that the random method does not update for each spawn but is set at the beginning of the game. I've searched a lot on the web and tried solutions that I though would work but no hope... So heres my code In the `LoadContent()` I have this code: ``` Random textureRandom = new Random(); int skinRandom = textureRandom.Next(1, 4); string lamp = string.Concat("image", skinRandom.ToString()); enemyTex = Content.Load<Texture2D>(lamp) as Texture2D; ```