好的,所以我试图在VB6中为一个类项目制作第三人称游戏,当这个人与一个墙(形状)碰撞时,他们就不应该移动。但问题是,当人碰撞到墙上时,它会停止,但现在墙壁现在被卡住了,并且不会与所有其他墙壁一起滚动。这是我的代码:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Or vbKeyRight Or vbKeyUp Or vbKeyDown Then
tmrMove.Enabled = True
End If
Select Case KeyCode
Case vbKeyLeft
XVel = 0 - Speed
YVel = 0
Case vbKeyRight
XVel = Speed
YVel = 0
Case vbKeyUp
YVel = 0 - Speed
XVel = 0
Case vbKeyDown
YVel = Speed
XVel = 0
End Select
Keys(KeyCode) = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = False
If Keys(vbKeyLeft) = False And Keys(vbKeyRight) = False And Keys(vbKeyUp) = False And Keys(vbKeyDown) = False Then
XVel = 0
YVel = 0
End If
End Sub
Private Sub tmrMove_Timer()
For i = 0 To (Wall.Count - 1)
If Collision(Character, Wall(i)) = False Then
Wall(i).Left = Wall(i).Left - XVel
Wall(i).Top = Wall(i).Top - YVel
End If
Next i
End Sub
Public Function Collision(Shape1 As ShockwaveFlash, Shape2 As Shape) As Boolean
If (Shape1.Left + Shape1.Width) > Shape2.Left And _
Shape1.Left < (Shape2.Left + Shape2.Width) And _
(Shape1.Top + Shape1.Height) > Shape2.Top And _
Shape1.Top < (Shape2.Top + Shape2.Height) Then
Collision = True
Else
Collision = False
End If
End Function
现在你可以看到,问题是,当它发生碰撞时,我不知道如何“uncollide”,所以我们碰撞的墙壁会卡住,不会与剩下的东西一起滚动。希望你明白解释是很困惑的。感谢
如您所见,
答案 0 :(得分:0)
修复碰撞逻辑最直接的方法是考虑问题:
而不是“我是否与墙壁相撞?”这个问题。
您可以通过将移动后的位置与墙壁施加的限制进行比较来回答这些问题。
代码示例(善良......过去10年我没有写VB6; - )
Public Function CanMoveLeft(Shape1 As ShockwaveFlash, Shape2 As Shape) As Boolean
If (Shape1.Left + Shape1.Width) > Shape2.Right)
Then
CanMoveLeft = True
Else
CanMoveLeft = False
End If
End Function
此示例假设您已将建议的新职位应用于Shape1
。如果您愿意,可以将未移动的Shape1
与左向速度一起传递,并相应地修改计算。我想你可能想要将形状的左边缘与墙壁的右边边缘进行比较,而不是比较代码示例中墙的左边缘。
请注意,如果您的移动后位置会将您置于墙内,您可能希望将实际位置调整到房间内(如果您每帧移动多个像素,那么您的速度可以放在墙内或墙外的当前位置。)