lecture roger sutton [email protected] co331 visual programming 16: timer control 1
Post on 19-Dec-2015
214 views
TRANSCRIPT
![Page 2: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/2.jpg)
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.
![Page 3: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/3.jpg)
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
![Page 4: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/4.jpg)
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
![Page 5: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/5.jpg)
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
![Page 6: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/6.jpg)
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
![Page 7: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/7.jpg)
CO331 Visual Programming
Example – cont’d
Implementation:
7
![Page 8: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/8.jpg)
CO331 Visual Programming
Example – cont’d
Implementation – cont’d:
8
![Page 9: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/9.jpg)
CO331 Visual Programming
Other examples
1. Circular movement
9
Run
![Page 10: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/10.jpg)
CO331 Visual Programming
Other examples – cont’d
2. Wave/Bounce
10
Run
![Page 11: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/11.jpg)
CO331 Visual Programming
Other examples – cont’d
Wave/bounce ?
11
![Page 12: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/12.jpg)
CO331 Visual Programming
Other examples – cont’d
3. Spiral
12
Run
![Page 13: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/13.jpg)
CO331 Visual Programming
Other examples – cont’d
4. Rose
13
Run
![Page 14: Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1](https://reader030.vdocument.in/reader030/viewer/2022032703/56649d2e5503460f94a0600c/html5/thumbnails/14.jpg)
CO331 Visual Programming
Other examples – cont’d
14