|
-
May 9th, 2013, 05:39 AM
#1
Thread Starter
PowerPoster
[RESOLVED] [VB6] - wall collision precision
is these function correct for test the collision?
Code:
Public Function TestCollision(X1 As Long, Y1 As Long, Width1 As Long, Height1 As Long, X2 As Long, Y2 As Long, Width2 As Long, Height2 As Long) As Boolean
If (X1 + Width1 >= X2 And X1 <= X2 + Width2) And (Y1 + Height1 >= Y2 And Y1 <= Y2 + Height2) Then
TestCollision = True
Else
TestCollision = False
End If
End Function
i'm trying use it, but isn't working
Code:
Private Type CollisionDirections
Left As Boolean
Right As Boolean
Up As Boolean
Down As Boolean
End Type
Const PlayerSpeed As Integer = 5
Dim dirCollision As CollisionDirections
Private Sub sprPlayer_KeyDown(KeyCode As Integer, Shift As Integer, KeyDownTime As Long)
Dim i As Integer
If (KeyCode = vbKeyLeft) Then
For i = 0 To sprWall.Count - 1
If (sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) = True) Then
dirCollision.Left = True
Else
dirCollision.Left = False
End If
Next i
If dirCollision.Left = False Then sprPlayer.Left = sprPlayer.Left - PlayerSpeed
.......
anyone can tell me what i'm doing wrong?
-
May 9th, 2013, 10:07 AM
#2
Re: [VB6] - wall collision precision
You've got the loop wrong... as you will keep setting dirCollision.Left for each wall, so it will only store the last value.
Here is one way you could fix it:
Code:
If (KeyCode = vbKeyLeft) Then
dirCollision.Left = False
For i = 0 To sprWall.Count - 1
If sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) Then
dirCollision.Left = True
Exit For
End If
Next i
-
May 9th, 2013, 10:27 AM
#3
Thread Starter
PowerPoster
Re: [VB6] - wall collision precision
 Originally Posted by si_the_geek
You've got the loop wrong... as you will keep setting dirCollision.Left for each wall, so it will only store the last value.
Here is one way you could fix it:
Code:
If (KeyCode = vbKeyLeft) Then
dirCollision.Left = False
For i = 0 To sprWall.Count - 1
If sprPlayer.TestCollision(sprPlayer.Left - PlayerSpeed, sprPlayer.Top, sprPlayer.Width, sprPlayer.Height, sprWall(i).Left, sprWall(i).Top, sprWall(i).Width, sprWall(i).Height) Then
dirCollision.Left = True
Exit For
End If
Next i
thanks.. it's working
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|