I am making a prototype for a game, and am trying to make it so that the characters have spawned in a number of random places, and each will get a random X and Y coordinate that they will go to, arrive, and move to a new one. So far, I am having an issue where they all receive the same coordinates to travel to, and not getting new coordinates either. Any help would be greatly appreciated. Below is a rough code reading of what I have done so far:
private void MakeShootEnemies() //creates the function for shootEnemy creation
{
PictureBox shootEnemy = new PictureBox(); //initialises the creation of picturebox
shootEnemy.Tag = "shootEnemy"; //tags the picturebox
shootEnemy.Image = Properties.Resources.RangeScientistTransparent; //inserts the scientist image to the picture box
shootEnemy.Left = randNum.Next(100, 1820); //creates a random x coordinate for the enemy to spawn on
shootEnemyLeft = shootEnemy.Left;
shootEnemy.Top = randNum.Next(100, 920); //creates a random y coordinate for the enemy to spawn on
shootEnemyTop = shootEnemy.Top;
Size size = new Size(75, 100); //creates a size for the box to be
shootEnemy.Size = size; //sets the box to said size
shootEnemy.SizeMode = PictureBoxSizeMode.StretchImage; //makes the image fit to the picturebox
shootEnemy.BackColor = Color.Transparent; //should make the background transparent (doesnt work and will probably remove)
shootEnemiesList.Add(shootEnemy); //adds enemy to enemies list
this.Controls.Add(shootEnemy); //adds the controls for the enemy to the form
player.BringToFront(); //brings the player to the front of the screen
healthBar.BringToFront();
shootXDestination = randNum.Next(100, 1820);
shootYDestination = randNum.Next(100, 920);
}
^Function that creates the enemies^
private void ShootEnemyMovement()
{
foreach (Control y in this.Controls)
{
shootXDestination = randNum.Next(0, 1820);
shootYDestination = randNum.Next(100, 920);
}
}
^Function that should give a new random value when arriving at the previous destination^
I have also tried to do this with the randNum function within the spawning of the enemies, as shown below:
for (int i = 0; i < 2; i++) //spawns the number of shoot enemies needed to take the total enemy count to 10
{
MakeShootEnemies();
shootXDestination = randNum.Next(100, 1820);
shootYDestination = randNum.Next(100, 920);
}
(number will differ in future but this is for testing purposes)
The randNum function is initialized as follows:
Random randNum = new Random();
Below is the movement itself:
foreach (Control y in this.Controls) //makes the shoot enemies easier to control
{
if (y is PictureBox && (string)y.Tag == "shootEnemy")
{
if ((y.Left == shootXDestination && y.Top == shootYDestination) || (y.Left == shootEnemyLeft && y.Top == shootEnemyTop))
{
ShootEnemyMovement();
}
if (y.Left > shootXDestination)
{
y.Left -= enemySpeed;
}
if (y.Left < shootXDestination)
{
y.Left += enemySpeed;
}
if (y.Top > shootYDestination)
{
y.Top -= enemySpeed;
}
if (y.Top < shootYDestination)
{
y.Top += enemySpeed;
}
}
}