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 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()
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 mintTimeElapsed As Integer = 0 ' elapsed time in seconds
. . .
Private Sub tmrGameTimer_Tick(. . .)
mintTimeElapsed += 1
lblTime.Text = "Total Time: " + Str(mintTimeElapsed)
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 mintTimeAllowed As Integer = 120 ' player must complete game in 120 seconds
. . .
Private Sub tmrGameTimer_Tick(. . .)
mintTimeAllowed -= 1
lblTime.Text = "Time Remaining: " + Str(mintTimeAllowed)
If (mintTimeAllowed <= 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 tmrEnemy1_Tick(. . .)
picEnemy1.Left = picEnemy1.Left + 10
End Sub
- A Timer can be used to make a picturebox move horizontally to the right and wrap around the form so that it comes in from the left
Private Sub tmrEnemy1_Tick(. . .)
picEnemy1.Left = picEnemy1.Left + 10
If (picEnemy1.Right > 300) Then
picEnemy1.Left = 0
End If
End Sub
- A Timer can be used along with a module variable to make a picturebox move horizontally to the right and bounce off the right edge of the form
Dim mstrDirectionEnemy1 As String = "right"
Private Sub tmrEnemy1_Tick(. . .)
If (mstrDirectionEnemy1 = "right") Then
picEnemy1.Left += 10
ElseIf (mstrDirectionEnemy1 = "left") Then
picEnemy1.Left -= 10
End If
If (picEnemy1.Right > 300) Then
mstrDirectionEnemy1 = "left"
ElseIf (picEnemy1.Left < 0) Then
mstrDirectionEnemy1 = "right"
End If
End Sub
- It is effective to type animation and collision detection code into the Tick method of a Timer. The following code segment detects a missile picturebox colliding with an enemy picturebox.
Private Sub tmrEnemy1_Tick(...)
picEnemy1.Left = picEnemy1.Left + 10
' wrap enemy around the screen
If (picEnemy1.Left > 300) Then
picEnemy1.Left = 0
End If
End Sub
Private Sub tmrMissile1_Tick(...)
picMissile1.Top = picMissile1.Top - 10
' reset missile if it misses the enemy
If (picMissile1.Bottom < 0) Then
picMissile1.Top = 111 ' reset the missile
tmrMissile1.Stop()
End If
' missile hits enemy
If (picEnemy1.Left < picMissile1.Left And picEnemy1.Right > picMissile1.Right And picMissile1.Top < picEnemy1.Bottom) Then
Me.Text = Val(Me.Text) + 1
picMissile1.Top = 111 'reset the missile
tmrMissile1.Stop()
End If
End Sub