What should I do when I want to give random moves to these enemies as they're coming down in a straight line?
enemyOne.Top = enemySpeed;
enemyTwo.Top = enemySpeed;
enemyThree.Top = enemySpeed;
bossMonster.Top = enemySpeed;
ambulance.Top = enemySpeed;
addBullet.Top = enemySpeed;
if (enemyOne.Top > 500)
{
enemyOne.Top = -650;
enemyOne.Left = rnd.Next(20, 450);
}
if (enemyTwo.Top > 500)
{
enemyTwo.Top = -650;
enemyTwo.Left = rnd.Next(20, 450);
}
if (enemyThree.Top > 500)
{
enemyThree.Top = -650;
enemyThree.Left = rnd.Next(20, 450);
}
I want to give the direction of the enemy in a fluid way, not a fixed value.
CodePudding user response:
If you want the monster's movement to match your movement, you can set a tag and turn the appropriate switch case.
foreach (Control x in this.Controls)
{
if (x is PictureBox && (string)x.Tag == "zombie")
{
if (x.Left > player.Left)
{
x.Left -= enemySpeed;
((PictureBox)x).Image = Properties.Resources.bossM;
}
if (x.Left < player.Left)
{
x.Left = enemySpeed;
((PictureBox)x).Image = Properties.Resources.bossM;
}
}
}