判斷物件有沒有相撞:
站長不懂Flash,但聽說當中有個HHittest的函數,是用來判斷兩個物件有沒有相撞,VB本身沒有,於是站長自己寫一個出來: Function Hittest(ObjA As Object, ObjB As Object) As Boolean
'傳回True代表相撞
'傳回False代表沒有相撞
If (ObjA.Left + ObjA.Width >= ObjB.Left) And (ObjA.Left <= ObjB.Left + ObjB.Width) And (ObjA.Top + ObjA.Height >= ObjB.Top) And (ObjA.Top <= ObjB.Top + ObjB.Height) Then HitTest = True
End Function
用法範例:(準備Picture1, Picture2)
Dim CX As Single, CY As Single
Private Function HitTest(ObjA As Object, ObjB As Object) As Boolean
If (ObjA.Left + ObjA.Width >= ObjB.Left) And (ObjA.Left <= ObjB.Left + ObjB.Width) And (ObjA.Top + ObjA.Height >= ObjB.Top) And (ObjA.Top <= ObjB.Top + ObjB.Height) Then HitTest = True
End Function
Private Sub Form_Activate()
Do
DoEvents
If HitTest(Picture1, Picture2) = True Then
Me.Caption = "相撞中"
Else
Me.Caption = "沒有相撞"
End If
Loop
End Sub
Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
CX = X
CY = Y
End Sub
Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Picture2.Move Picture2.Left + X - CX, Picture2.Top + Y - CY
End Sub
貼上上面的程式碼後,嘗試拖曳Picture2到Picture1並看看表單標題吧!