VB Lecture Notes - Animation
Objective #1: Animate graphics on a form with For loops.
- You can achieve animation by simplying changing an object's Top or Left property within a loop. For example, the
following loop would animate the picObject horizontally across a form to the left:
For j = 1 To 5
picObject.Left = picObject.Left - 20
Next
or
For j = 1 To 5
picObject.Left -= 20
Next
making use of the += operator.
- You can create a pause with the statement
Threading.Thread.Sleep(1000)
where the parameter in the parentheses is the number of milliseconds. This statement makes the computer to stop for 1000 milliseconds (which is 1 second) each time it iterates the loop. So the following loop would make picObject animate more slowly
For j = 1 To 5
picObject.Left -= 20
Threading.Thread.Sleep(1000)
Next
- The
following loop would animate the picObject horizontally across the form to the right:
For j = 1 To 5
picObject.Left += 20
Threading.Thread.Sleep(1000)
Next
You can not use the Right property in this situation since the Right property is "read-only".
- The
following loop would animate the picObject up the form:
For j = 1 To 5
picObject.Top -= 20
Threading.Thread.Sleep(1000)
Next
Remember that the y-axis goes from 0 at the top to 300 at the bottom so you actually need to subtract instead of add to move upwards.
- The
following loop would animate the picObject down the form:
For j = 1 To 5
picObject.Top += 20
Threading.Thread.Sleep(1000)
Next
You can not use the Bottom property in this situation since the Bottom property is "read-only".
- By changing the amount pixels that you add or subtract (20 in this example) and by changing the numeric parameter in the Sleep method (1000 in this example), you can change the speed of the animation.
- You can change the distance of the animation by changing the amount of pixels that you add or subtract (20 in this example) and by changing the top boundary number of the For loop (5 in this example.)