Part 5 - Timer events
Copy and paste the fallowing code into Form1.
- Private Sub mobjTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles mobjTimer.Elapsed
- If mobjGraphics Is Nothing Then Exit Sub
- Try
- mobjGraphics.Graphics.Clear(Color.Black)
- DrawFlies()
- DrawReceptors()
- DrawScents()
- DrawActor(mobjPlayer)
- KeepInBounds()
- DecayFarts()
- mobjGraphics.Graphics.DrawString("Clickor press a key to exit...", Me.Font, Brushes.White, 50, 50)
- mobjGraphics.Render()
- mobjTimer.Start()
- Catch
- End Try
- End Sub
The
first thing we do is to check if the graphics variable has been set to
nothing. If it has we can exit. We do this check because the timer
objects we are using are running in the background and even if we were
to close the form and set all variables to nothing the next Elapsed
event will still be raised. Next we call the Clear method on the
graphics variable to clear the scene of what was drawn previously. Then
it proceeds to draw the flies, receptors, player, and scent objects
onto the buffered graphics variable we setup earlier.
The next two methods being called are KeepInBounds and DecayFarts. These methods will be covered later in the tutorial.
Next we draw a message on the screen for the user and then render out what we have drawn out to the form.
The
timer that is used to draw the graphics on screen has been setup to
raise the Elapsed event only once. So we must call mobjTimer.Start
again to receive another Elapsed event.
The fly
updater and stinky spawner timers are simply setup to call the
MoveFiles and MakeStinky methods. After that they call there Start
methods so that there Elapsed events will fire again.
Copy and paste the fallowing code into Form1
- Private Sub mobjStinkySpawner_Elapsed(ByVal sender As Object, ByVal eAs System.Timers.ElapsedEventArgs) Handles mobjStinkySpawner.Elapsed
- If mobjGraphics Is Nothing Then Exit Sub
- Try
- MakeStinky()
- mobjStinkySpawner.Start()
- Catch
- End Try
- End Sub
- Private Sub mobjFlyUpdater_Elapsed(ByVal sender As Object, ByVal e AsSystem.Timers.ElapsedEventArgs) Handles mobjFlyUpdater.Elapsed
- If mobjGraphics Is Nothing Then Exit Sub
- Try
- MoveFlies()
- mobjFlyUpdater.Start()
- Catch
- End Try
- End Sub