Wyo VB Lecture Notes
Objective #1 - Use Timer objects for animation.
- A Timer object can be used to animate objects without the user having to press keys. A Timer object has an Interval property which is a number of milliseconds. If
the timer's Enabled property is True, VB counts off this number of milliseconds and executes any code typed in the timer's Tick method and does so continuously until the timer is disabled.
- Since the Interval property is a number of milliseconds, you must set it to 1000 if you want the timer to execute every one second.
- The proper prefix for a timer is tmr. A timer does not appear on the interface of the form. You will see it below the form's interface in the component tray. Double-click the timer to open up the Code window
to its Tick method.
- A Timer can be enabled by setting its Enabled property to True or by calling its Start method.
tmrEnemy.Enabled = True
or
tmrEnemy.Start()
- A Timer can be disabled by setting its Enabled property to False or by calling its Stop method.
tmrEnemy.Enabled = False
or
tmrEnemy.Stop()
- Any code that is typed into the timer's Tick method will execute over and over again. The amount of time that transpires between the times that the code executes is the value of the Interval property in milliseconds. So if the timer's Interval property is set to 1000 then the code in the Tick method will execute every second.
Objective #2 - Use a Timer to display the elapsed time that it takes for the user to play a game.
- A Timer can be used to show the amount of elapsed time on the screen. By setting the Interval property to 1000 (i.e. one second) and using a module level variable, you can increment the variable each time the Timer ticks.
A Timer can be enabled by setting its Enabled property to True or by calling its Start method.
Dim mTimeElapsed As Integer = 0 ' elapsed time in seconds
Private Sub tmrGameTimer_Tick(. . .)
mTimeElapsed += 1
lblTime.Text = "Total Time: " + Str(mTimeElapsed)
End Sub
or you can count down the amount of time by initializing the module variable to a given number of seconds and decrement the variable in the Tick method.
Dim mTimeAllowed As Integer = 60 ' player must complete game in 120 seconds
Private Sub tmrGameTimer_Tick(. . .)
mTimeAllowed -= 1
lblTime.Text = "Time Remaining: " + Str(mTimeAllowed)
If (mTimeAllowed <= 0) Then
tmrGameTimer.Stop()
MessageBox.Show("Game Over")
End If
End Sub
Objective #3 - Use Timer objects to animate enemies in a game.
- A Timer can be used to make a picturebox move horizontally to the right
Private Sub tmrGhost_Tick(. . .)
picGhost.Left += 5
End Sub
- A Timer can be used to make a picturebox (such as an enemy fighter plane at the top of a form) move horizontally to the right AND wrap around the form so that it comes in from the left
Private Sub tmrEnemy_Tick(. . .)
picEnemy.Left += 10
If (picEnemy.Right > Me.Right) Then
picEnemy.Left = 0
End If
End Sub
Notice that Me.Right is used instead of 300 since the Form's width may not be exactly 300. You can also use picPlayer.Left = -picPlayer.Width instead of picPlayer.Left = 0 for more precision.
- Or, a Timer can be used to make a picturebox (such as a ghost in PacMan) follow another picturebox
Private Sub tmrGhost_Tick(. . .)
' ghost moves left to catch the player
If (picGhost.Left < picPlayer.Left) Then
picGhost.Left += 1
End If
' ghost moves right to catch the player
If (picGhost.Right > picPlayer.Right) Then
picGhost.Left -= 1
End If
' ghost moves up to catch the player
If (picGhost.Bottom > picPlayer.Bottom) Then
picGhost.Top -= 1
End If
' ghost moves down to catch the player
If (picGhost.Top < picPlayer.Top) Then
picGhost.Top += 1
End If
End Sub