Possible Duplicate:
Random number generator only generating one random number
Random not that random
This is my first attempt at using classes and objects and I have absolutely no clue as to how to handle the situation I'm currently in. I'm creating a game which requires the spawning of enemies at the edges of the screen, so I created a method that creates 10 objects and randomly selects an edge and a suitable coordinate to assign to them.
private void spawner()
{
if (enemies.Count < 10)
{
int a;
a = 10 - enemies.Count;
for (int i = 0; i < a; i++)
{
int x, y, c;
Random random = new Random();
c = random.Next(0, 4);
if (c == 0)
{
x = random.Next(0, 839);
y = 1;
}
else if (c == 1)
{
y = random.Next(0, 647);
x = 1;
}
else if (c == 2)
{
x = 839;
y = random.Next(0, 647);
}
else
{
y = 647;
x = random.Next(0, 839);
}
enemies.Add(new Enemy(5, 25, 3, new Point(x, y)));
enemies[enemies.Count - 1].adjustPosition(player1.centreX, player1.centreY);
}
}
}
I then call the method within the Enemy class to make the coordinates shift closer to the centre of the Player controlled object as well as create 3 new points that will be used to draw the enemy: enemies[enemies.Count - 1].adjustPosition(player1.centreX, player1.centreY);
public class Enemy
{
public int enemyLife { get; set; }
public Point enemyCentre { get; set; }
public int enemyRadius { get; set; }
public double enemySpeed { get; set; }
public Point[] enemyVertices { get; private set; }
public Enemy(int life, int radius, double speed, Point centre)
{
enemyLife = life;
enemyCentre = centre;
enemyRadius = radius;
enemySpeed = speed;
}
public void adjustPosition(int centreX, int centreY)
{
double vx = 0;
double vy = 0;
double mag = 0;
int x, y;
if (enemyCentre.X != 0)
{
vx = centreX - enemyCentre.X;
vy = centreY - enemyCentre.Y;
mag = Math.Sqrt(vx * vx + vy * vy); // length
vx /= mag;
vy /= mag;
x = (int)((double)enemyCentre.X + vx * enemySpeed);
y = (int)((double)enemyCentre.Y + vy * enemySpeed);
enemyCentre = new Point(x, y);
int px, py, px2, py2, px3, py3;
double angle;
vx = centreX - x;
vy = centreY - y;
mag = Math.Sqrt(vx * vx + vy * vy); // length
vx /= mag;
vy /= mag;
px = (int)((double)x + vx * 20);
py = (int)((double)y + vy * 20);
angle = Math.Atan2(centreY - y, centreX - x);
px2 = (int)(px + (-30 * Math.Cos(0.785398163 + angle)));
py2 = (int)(py + (-30 * Math.Sin(0.785398163 + angle)));
px3 = (int)(px + (-30 * Math.Cos(-0.785398163 + angle)));
py3 = (int)(py + (-30 * Math.Sin(-0.785398163 + angle)));
Point[] points = new Point[3];
points[0] = new Point(px, py);
points[1] = new Point(px2, py2);
points[2] = new Point(px3, py3);
enemyVertices = points;
}
}
}
The problem is that when I attempt to execute the program using 'Start Debugging' every created object shares the same centre coordinates with the the last added object to the list. Yet when I add a breakpoint before the objects are added to the list and Step through the code, the objects are assigned random values as they should be. Also if I remove the breakpoint after manually executing the code for some of the objects, the objects that been run manually will contain random values while the rest of the objects will share the same values. Does anyone know what could be causing this problem?