Game Development Reference
particular vehicle. We need to ask the vehicles to move because they hold their
position values internally. Switch to Vehicle.vb and add the following code:
Public Sub MoveForward(ByVal FrameRate As Integer)
Xpos += currentV / framerate
What remains is to ask the vehicles to move when the animation timer fires.
Switch back to the code for Road.vb and add the following code:
Private Sub AnimationTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles AnimationTimer.Tick
Dim Toy As Vehicle
'Increment drawn frames.
framecount += 1
Dim offset As Integer = CInt(Me.Width / 2)
'Move them forward and draw
For Each Toy In ToyBox
'Track the reference vehicle when we get it.
'Our floating marker will need to move when we put it in.
Before we run the code, a word or two about timers, frame rate, and perfor-
mance is in order. The timers we use have a maximum resolution of 55 milli-
seconds. This has an impact on how well the system can deliver the desired
frame rate. Running the code in the debugger will not help matters. Our two
timers will interact; the animation may lose smoothness when the AI runs.
While the VB code itself is reasonably fast, changing the positions of controls
using the native Windows desktop is a known choke point. The Microsoft
DirectX technology exists for this very reason. These timers should give rea-
sonable performance at our low frame rates. These timers do provide a number
of concrete benefits to the beginning AI programmer. These are the simplest
timers available. They let us control the think rate and the frame rate inde-
pendently. We do not have to deal with threading issues or the need to write a
time-locked core graphics loop.