'Class module
Sub Class_Globals
Private MyRect As Rect
Private Others As List
Private checkInterval, interval As Int
Private outRect(1) As Rect
Private safeZone As Int
End Sub
Public Sub Initialize (vMyRect As Rect, vOthers As List, vCheckInterval As Int, vSafeZone As Int)
MyRect = vMyRect
'Others is a list of objects. Each object should have a GetRect sub such as:
'Public Sub GetRect(outRect() As Rect)
' outRect(0) = bd.DestRect
'End Sub
'Note that this class holds a reference to the list. This means that if later an object is added or removed
'then it will automatically affect this class as well.
Others = vOthers
'Check for collision every x ticks. If the objects do not move too fast there is no point
'in checking for collision every tick.
checkInterval = vCheckInterval
'As each sprite is treated as a rectangle you may want to decrease the rectangle size to avoid cases
'where the collision is in two corners for example.
safeZone = vSafeZone
End Sub
'Checks for collisions. Returns the collided object if there was a collision
Public Sub Tick As Object
interval = interval + 1
If interval = checkInterval Then
interval = 0
Dim r As Rect
For i = 0 To Others.Size - 1
CallSub2(Others.Get(i), "GetRect", outRect)
If MyRect.Left < outRect(0).Right - safeZone AND MyRect.Right > outRect(0).Left + safeZone _
AND MyRect.Top < outRect(0).Bottom - safeZone AND MyRect.Bottom > outRect(0).Top + safeZone Then
Return Others.Get(i)
End If
Next
Return Null
End If
End Sub