Visual Basic animation with interactive demo and VB.NET implementation
This enhanced animation program demonstrates how to create movement in Visual Basic using timers and the Move method. In this program, you need to insert four timers and make them invisible with their Enabled property set to false at startup. The program uses the Move method and the computer coordinate system (Left, Top) to change the positions of the image.
The basic syntax of the Move method is object.Move Left, Top. For example, Image1.Move 1000,1000 will move the object Image1 to the location (1000,1000). I've created four sub procedures Move_up, Move_down, Move_left and Move_right to move the image in the respective directions.
Try the animation below! Click the direction buttons to move the box. The box will reset to the center when it reaches the boundary.
Dim x As Integer Dim y As Integer Dim v, w As Integer Static Sub move_right() v = v + 10 If v < 5350 Then Image1.Move v, w Else Image1.Move 2640, 1560 v = 2640 w = 1560 Timer1.Enabled = False End If End Sub Public Sub Command1_Click(Index As Integer) Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False Timer1.Enabled = True End Sub Private Sub move_down() w = w + 10 If w < 3670 Then Image1.Move v, w Else Image1.Move 2640, 1560 v = 2640 w = 1560 Timer2.Enabled = False End If End Sub Private Sub move_left() v = v - 10 If v > 0 Then Image1.Move v, w Else Image1.Move 2640, 1560 v = 2640 w = 1560 Timer3.Enabled = False End If End Sub Private Sub move_up() w = w - 10 If w > 0 Then Image1.Move v, w Else Image1.Move 2640, 1560 v = 2640 w = 1560 Timer4.Enabled = False End If End Sub Private Sub Timer2_Timer() move_down End Sub Private Sub Timer3_Timer() move_left End Sub Private Sub Timer4_Timer() move_up End Sub
Public Class AnimationForm Private v As Integer = 2640 Private w As Integer = 1560 Private stepSize As Integer = 10 Private Sub MoveRight() v += stepSize If v < Me.ClientSize.Width - Image1.Width Then Image1.Left = v Else ResetPosition() Timer1.Enabled = False End If End Sub Private Sub MoveDown() w += stepSize If w < Me.ClientSize.Height - Image1.Height Then Image1.Top = w Else ResetPosition() Timer2.Enabled = False End If End Sub Private Sub MoveLeft() v -= stepSize If v > 0 Then Image1.Left = v Else ResetPosition() Timer3.Enabled = False End If End Sub Private Sub MoveUp() w -= stepSize If w > 0 Then Image1.Top = w Else ResetPosition() Timer4.Enabled = False End If End Sub Private Sub ResetPosition() v = 2640 w = 1560 Image1.Left = v Image1.Top = w End Sub ' Button Click Handlers Private Sub btnRight_Click(sender As Object, e As EventArgs) Handles btnRight.Click Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False Timer1.Enabled = True End Sub Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click Timer1.Enabled = False Timer3.Enabled = False Timer4.Enabled = False Timer2.Enabled = True End Sub Private Sub btnLeft_Click(sender As Object, e As EventArgs) Handles btnLeft.Click Timer1.Enabled = False Timer2.Enabled = False Timer4.Enabled = False Timer3.Enabled = True End Sub Private Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click Timer1.Enabled = False Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = True End Sub Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click Timer1.Enabled = False Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False End Sub ' Timer Handlers Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick MoveRight() End Sub Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick MoveDown() End Sub Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick MoveLeft() End Sub Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick MoveUp() End Sub End Class