Post

 Resources 

Console

Fart Sniffer


Part 5 - Timer events


Copy and paste the fallowing code into Form1.

  1. Private Sub mobjTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles mobjTimer.Elapsed
  2.    If mobjGraphics Is Nothing Then Exit Sub
  3.    Try
  4.       mobjGraphics.Graphics.Clear(Color.Black)
  5.       DrawFlies()
  6.       DrawReceptors()
  7.       DrawScents()
  8.       DrawActor(mobjPlayer)
  9.       KeepInBounds()
  10.       DecayFarts()
  11.       mobjGraphics.Graphics.DrawString("Clickor press a key to exit...", Me.Font, Brushes.White, 50, 50)
  12.       mobjGraphics.Render()
  13.       mobjTimer.Start()
  14.    Catch
  15.    End Try
  16. 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

  1. Private Sub mobjStinkySpawner_Elapsed(ByVal sender As Object, ByVal eAs System.Timers.ElapsedEventArgs) Handles mobjStinkySpawner.Elapsed
  2.    If mobjGraphics Is Nothing Then Exit Sub
  3.    Try
  4.       MakeStinky()
  5.       mobjStinkySpawner.Start()
  6.    Catch
  7.    End Try
  8. End Sub
  9. Private Sub mobjFlyUpdater_Elapsed(ByVal sender As Object, ByVal e AsSystem.Timers.ElapsedEventArgs) Handles mobjFlyUpdater.Elapsed
  10.    If mobjGraphics Is Nothing Then Exit Sub
  11.    Try
  12.       MoveFlies()
  13.       mobjFlyUpdater.Start()
  14.    Catch
  15.    End Try
  16. End Sub




Next: Part 6 - Keeping things in view | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Post a Comment


Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 29 visitors within the last 20 minutes
RSS News Feed