I was working on a game where I have a bunch of enemies. I wanted the game to randomly select which enemy to use but it always picks the same one. Here is the file were I make the enemies:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack
{
public class Enemy : Sprite
{
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
// which direction are we moving through the animation set?
private int direction = 1;
double lastTime;
public Enemy(ContentManager contentManager)
{
spriteTextures = new Texture2D[10];
// load the spriteTextures
if(RandomNumber(1,2) == 1)
for (int i = 0; i <= 9; ++i)
spriteTextures[i] = contentManager.Load<Texture2D>("gfx\\enemy1\\enemy1_" + i);
if(RandomNumber(1,2) == 2)
for (int i = 1; i <= 9; ++i)
spriteTextures[i] = contentManager.Load<Texture2D>("gfx\\enemy2\\enemy2_" + i);
this.Velocity.X = 1;
}
public override void Update(GameTime gameTime)
{
// if we're at the end of the animation, reverse direction
if(frameIndex == 9)
direction = -1;
// if we're at the start of the animation, reverse direction
if(frameIndex == 0)
direction = 1;
// every 70ms, move to the next frame
if(gameTime.TotalGameTime.TotalMilliseconds - lastTime > 70)
{
frameIndex += direction;
lastTime = gameTime.TotalGameTime.TotalMilliseconds;
}
}
}
}
And then here is the code that loads these enemies into the game:
enemies = new Enemy[EnemyRows,EnemyCols];
// create a grid of enemies
for(int y = 0; y < EnemyRows; y++)
{
for(int x = 0; x < EnemyCols/2; x=x+2)
{
Enemy enemy = new Enemy(contentManager);
enemy.Position.X = x * enemy.Width + EnemySpacing.X;
enemy.Position.Y = y * enemy.Height + EnemySpacing.Y;
enemies[y,x] = enemy;
}
}
Any ideas on what is wrong?