Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Roger Sutton CO331 Visual Programming 16: Timer Control 1.

Similar presentations


Presentation on theme: "Lecture Roger Sutton CO331 Visual Programming 16: Timer Control 1."— Presentation transcript:

1 Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 16: Timer Control 1

2 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.

3 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

4 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

5 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

6 CO331 Visual Programming Example – cont’d Algorithm: 1.Set direction to right Each tick 2.Display box 3.Display ball 4.Move ball 5 pixels in direction of movement 5.If x-location of ball greater than 290 set direction to left 6.If x-location of ball less than 10 set direction to right 6 Form Design: cmdStart Ticker cmdExit picbox1

7 CO331 Visual Programming Example – cont’d Implementation: 7

8 CO331 Visual Programming Example – cont’d Implementation – cont’d: 8

9 CO331 Visual Programming Other examples 1.Circular movement 9 Run

10 CO331 Visual Programming Other examples – cont’d 2. Wave/Bounce 10 Run

11 CO331 Visual Programming Other examples – cont’d Wave/bounce ? 11

12 CO331 Visual Programming Other examples – cont’d 3.Spiral 12 Run

13 CO331 Visual Programming Other examples – cont’d 4. Rose 13 Run

14 CO331 Visual Programming Other examples – cont’d 14


Download ppt "Lecture Roger Sutton CO331 Visual Programming 16: Timer Control 1."

Similar presentations


Ads by Google