2011-10-11 40 views
1

好吧,所以我想在VB6中为班级项目制作第三人称游戏,并且当人与墙(形状)碰撞时,他们不应该移动。但问题是,当人碰撞到墙壁时,它会停下来,但现在墙壁已经卡住,不会与所有其他墙壁一起滚动。这里是我的代码:在第三人称游戏中发生墙壁碰撞的困难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 

现在你可以看到,问题是,当发生碰撞时,我不知道HOWTO“uncollide”让我们与碰撞壁卡住,不会与其余滚动这些事。解释希望你理解是令人困惑的。由于

正如你所看到的,

回答

0

最直接的方法来解决你的碰撞的逻辑是要考虑的问题:

  • 我可以动起来?
  • 我可以向下移动吗?
  • 我可以向左移动吗?
  • 我可以向右移动吗?

而不是问题:“我在与墙相撞吗?”

你通过比较你的移动后位置和墙的限制来回答这些问题。

代码示例(善待...我没有在过去的10年;-)

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 

这个例子写VB6假定您已经申请提议的新位置Shape1。如果你愿意,你可以通过不动的Shape1以及向左的速度并相应地修改计算。我想你可能想要比较形状的左边缘和墙的边缘,而不是代码示例中墙的左边缘。

请注意,如果您的移动后位置会将您置于墙内,则您需要将实际位置调整到房间内(如果您每帧移动多个像素,则速度可能会提高你目前的位置在墙内或墙外)。