lecture roger sutton [email protected] co331 visual programming 16: timer control 1
Post on 19-Dec-2015
216 views
TRANSCRIPT
CO331 Visual Programming
Introduction
The timer is an example of a component, as opposed to a control. When it is dragged from the Toolbox to a form, the IDE opens up a Component Tray window to hold the time icon. It is not visible when the program executes.
2
A timer enables methods to be executed periodically and facilitates animation.
CO331 Visual Programming
Timer Class
A timer creates ‘ticks’ at regular intervals. Each tick is an event which calls the Tick method.• The Interval property is set to an integer value representing the time
between ticks in milliseconds.Thus if set to 1000, ticks occur every second.
Care should be taken when deciding on the Interval setting as too short a time will inhibit interaction.
• A timer is started and stopped using the Start and Stop methods. These set the Enabled property to True or False programmatically.
• Several timers may used in a program, each with a different interval.
3
Default interval 100 milliseconds
CO331 Visual Programming
Animation
To create movement requires a picture to be displayed and then re-displayed repeatedly with slight modifications each time.Example:Consider the task of displaying a ball moving from side to side within box.
Two factors need to be considered:• How great should be the modification?• What time interval should there be between successive pictures?
Together these factors govern the speed of movement and the smoothness of continuity.
4
Run
CO331 Visual Programming
Animation – cont’d
Suppose• the ball moves 5 pixels at a time, and• The picture is redisplayed every 50 milliseconds
(can be modified later if necessary)Let the box be 300 pixels wide and 150 deep.Suppose the ball is 20 pixels in diameter and located by the position of its centre. Accordingly it will start at location (10,75), 10 pixels across and 75 down, and reach the right-hand side at (290,75). At this point the ball will change direction.Essentially the vertical position remains fixed and the horizontal movement changes according to the direction of movement.
The next stage is to construct an algorithm.
5
CO331 Visual Programming
Example – cont’d
Algorithm:1. Set direction to rightEach tick2. Display box3. Display ball4. Move ball 5 pixels in direction
of movement5. If x-location of ball greater
than 290 set direction to left6. If x-location of ball less than
10 set direction to right
6
Form Design:
cmdStartTicker cmdExit
picbox1
CO331 Visual Programming
Example – cont’d
Implementation:
7
CO331 Visual Programming
Example – cont’d
Implementation – cont’d:
8
CO331 Visual Programming
Other examples
1. Circular movement
9
Run
CO331 Visual Programming
Other examples – cont’d
2. Wave/Bounce
10
Run
CO331 Visual Programming
Other examples – cont’d
Wave/bounce ?
11
CO331 Visual Programming
Other examples – cont’d
3. Spiral
12
Run
CO331 Visual Programming
Other examples – cont’d
4. Rose
13
Run
CO331 Visual Programming
Other examples – cont’d
14