我正在制作一个战舰游戏,我使用按钮作为网格(播放器)。我正在使用图片框作为船,并且我正在尝试使图片框捕捉到它正在碰撞的按钮。
我已经完成了拖放和碰撞部分,但我正在努力处理按钮部分。图片框是两个按钮的大小。我尝试使用picturebox.left = button.left将图片框与按钮对齐,但它选择了两者中的错误按钮。
Dim Off As Point
Private Sub picDestroyer_MouseDown(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseDown
Off.X = MousePosition.X - sender.Left 'Click and Drag ship
Off.Y = MousePosition.Y - sender.Top
End Sub
Private Sub picDestroyer_MouseMove(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseMove
If e.Button = MouseButtons.Left Then
sender.Left = MousePosition.X - Off.X 'Click and Drag ship
sender.Top = MousePosition.Y - Off.Y
End If
End Sub
Private Sub picDestroyer_DoubleClick(sender As Object, e As EventArgs) Handles picDestroyer.DoubleClick
If picDestroyer.Size = New Size(52, 21) Then 'Rotate Pic if double clicked
picDestroyer.Size = New Size(21, 52)
ElseIf picDestroyer.Size = New Size(21, 52) Then
picDestroyer.Size = New Size(52, 21)
End If
End Sub
Private Sub picDestroyer_MouseLeave(sender As Object, e As EventArgs) Handles picDestroyer.MouseLeave
For Each button In Me.Controls
If picDestroyer.Bounds.IntersectsWith(button.bounds) Then
button.backcolor = Color.Red
picDestroyer.BackColor = SystemColors.Control
End If
If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(52, 21) Then
picDestroyer.Top = button.top
End If
If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(21, 52) Then
picDestroyer.Left = button.left
End If
Next