vb设计的两个标签碰撞问题,怎样区分两种接触条件
Dim j, i, m, n As Integer
Private Sub Form_Load()
i = 1
j = 1
m = 1
n = 1
End Sub
Private Sub Timer1_Timer()
If (Label1.Left = 0) Then '控制label1左方边界
i = i * -1
End If
If (Label1.Left + 1.1 * Label1.Width >= Form1.Width) Then '控制label1右方边界
i = i * -1
End If
If (Label1.Top = 0) Then '控制label1上方边界
j = j * -1
End If
If (Label1.Top + 2 * Label1.Height >= Form1.Height) Then '控制label1下方边界
j = j * -1
End If
If (Label2.Left = 0) Then '控制label2左方边界
m = m * -1
End If
If (Label2.Left + 1.1 * Label2.Width >= Form1.Width) Then '控制label2右方边界
m = m * -1
End If
If (Label2.Top = 0) Then '控制label2上方边界
n = n * -1
End If
If (Label2.Top + 2 * Label2.Height >= Form1.Height) Then '控制label2下方边界
n = n * -1
End If
If (Abs(Label1.Left - Label2.Left) <= (Abs(Label1.Width)) And Abs(Label1.Top - Label2.Top) < Abs(Label1.Height)) Then '控制两标签左右边界相撞的情况
i = i * -1
m = m * -1
End If
If (Abs(Label1.Left - Label2.Left) < Abs(Label1.Width) And Abs(Label1.Top - Label2.Top) <= (Abs(Label1.Height))) Then '控制两标签上下边界相撞的情况
j = j * -1
n = n * -1
End If
Label1.Left = Label1.Left + 40 * i
Label1.Top = Label1.Top + 40 * j
Label2.Left = Label2.Left - 30 * m
Label2.Top = Label2.Top - 40 * n
End Sub
我设计的两标签碰撞分两种情况,如果左右边界碰撞,则只改变各自的左右方向的速度,而上下运动方向不变,如果上下边界碰撞,则只改变各自的上下运动方向,左右不变。问题在于这两种的情况区分条件很相近,无法区分,所以碰撞后两个运动方向都改变了。请高手指点一下,该如何写区分条件。
[解决办法]
Dim j, i, m, n As IntegerPrivate Sub Form_Load()i = 1j = 1m = 1n = 1Text1.Width = Text2.WidthText1.Height = Text2.HeightEnd SubPrivate Sub Timer1_Timer()If (Text1.Left <= 0) Then '控制text1左方边界i = i * -1End IfIf (Text1.Left + 1.1 * Text1.Width >= Form1.Width) Then '控制text1右方边界i = i * -1End IfIf (Text1.Top <= 0) Then '控制text1上方边界j = j * -1End IfIf (Text1.Top + 2 * Text1.Height >= Form1.Height) Then '控制text1下方边界j = j * -1End IfIf (Text2.Left <= 0) Then '控制text2左方边界m = m * -1End IfIf (Text2.Left + 1.1 * Text2.Width >= Form1.Width) Then '控制text2右方边界m = m * -1End IfIf (Text2.Top <= 0) Then '控制text2上方边界n = n * -1End IfIf (Text2.Top + 2 * Text2.Height >= Form1.Height) Then '控制text2下方边界n = n * -1End IfIf Abs(Text1.Top - Text2.Top) <= Abs(Text1.Height) + 50 Then If Abs(Text1.Left - Text2.Left) <= Abs(Text1.Width) Then j = j * -1 n = n * -1 GoTo Move End IfEnd IfIf Abs(Text1.Left - Text2.Left) <= Abs(Text1.Width) + 50 Then If Abs(Text1.Top - Text2.Top) <= Abs(Text1.Height) Then i = i * -1 m = m * -1 End IfEnd IfMove:Text1.Left = Text1.Left + 40 * iText1.Top = Text1.Top + 40 * jText2.Left = Text2.Left - 40 * mText2.Top = Text2.Top - 40 * nEnd Sub