Em có class FlyObject như sau
Mã:
public class FlyObject:PictureBox
{
public Point P1,P2;
public int Speed;
Random Rand;
Rectangle ClientRec;
public FlyObject(Point t,int r)
{
ClientRec=new Rectangle(0,0,640,640);
Speed = 4;
P1 = t;
Rand = new Random();
this.Location = P1;
this.Enabled = false;
}
public int FindY(Point MyPoint1, Point MyPoint2, int xValue)
{
//do something to find y
}
public delegate void FlyDelegate();
public void _Fly()
{
if(InvokeRequired)
{
this.Invoke(new FlyDelegate(Fly));
}
else
{
Fly();
}
}
public void Fly()
{
while (true)
{
do
{
P2.X = Rand.Next(ClientRec.Width);
}while(P2.X<P1.X);
P2.Y = Rand.Next(ClientRec.Height);
int x=P1.X;
while(x<P2.X)
{
y = FindY(P1, P2, x);
Location = new Point(x, y);
x = x + Speed;
System.Threading.Thread.Sleep(20);
}
}
P1 = P2;
}
}
}
và các hàm ở class Form 1 như sau
Mã:
private void MakeItFly_Thread()
{
System.Threading.Thread t1 = new System.Threading.Thread(MakeItFly1);
t1.Start();
System.Threading.Thread t2 = new System.Threading.Thread(MakeItFly2);
t2.Start();
System.Threading.Thread t3 = new System.Threading.Thread(MakeItFly3);
t3.Start();
System.Threading.Thread t4 = new System.Threading.Thread(MakeItFly4);
t4.Start();
}
private void MakeItFly1()
{
FlyObject1._Fly();
}
private void MakeItFly2()
{
FlyObject2._Fly();
}
private void MakeItFly3()
{
FlyObject3._Fly();
}
private void MakeItFly4()
{
FlyObject4._Fly();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MakeItFly_Thread();
}
khi chạy chỉ có FlyObject1 di chuyển, còn FlyObject2,3,4 vẫn đứng yên, mong mọi người giúp em làm cho 1 2 3 4 di chuyển cùng lúc...many thanks
View more random threads:
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ