In my program I am creating PictureBoxes which move across the screen. I want to make it so that the user can create as many of these as they want. To do this I created a class is assigned to a picturebox after it is created and controls it's movement. This works for the picturebox until I create another one, when it is no longer controlled. I assume this is because c# does not allow me to create multiple objects of a class, and therefore ends the previous ones to make a new one. Here's how I've done it:
static ArrayList cps = new ArrayList();
public void ShootCannon() {
Image cubeImage= Image.FromFile("C:\\Users\\Stefan\\Documents\\Visual Studio 2010\\Projects\\Game1\\Game1\\Resources\\CannonCube.png");
PictureBox cannonCube = new PictureBox();
ScreenPanel.Controls.Add(cannonCube);
cannonCube.Image = cubeImage;
cannonCube.SetBounds(cannonCubeInst.X, cannonCubeInst.Y, cubeImage.Width, cubeImage.Height);
cannonCube.BringToFront();
cps.Add(new CubeProjectile(cannonCube));
}
And the CubeProjectile class is:
public class CubeProjectile
{
static PictureBox box;
public CubeProjectile(PictureBox box1)
{
box = box1;
Timer Update = new Timer();
Update.Interval = 1;
Update.Tick += new EventHandler(Timer_Tick);
Update.Start();
}
void Timer_Tick(object sender, EventArgs e)
{
Point loc = new Point(box.Location.X, box.Location.Y);
box.SetBounds(loc.X + 1, loc.Y, box.Width, box.Height);
}
}