Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Using a Timer to Automate Animation

Although the Spin Globe program is fun, it would be better if the animation could happen automatically, without the user’s having to do anything. You can make this automatic animation happen by employing one new control to the Spin Globe program. The Auto Spin program shown in Figure 7.10 works much like the Spin Globe program but doesn’t have any buttons at all. Instead, a new type of invisible control, named the timer, automates the swapping of images.

Figure 7.10: You can’t see it here, but the image changes 10 times per second.

Introducing the Timer Control

The timer control is another control that is visible when you design a form but invisible to the end user. Like the ImageList, it appears in a special offstage area at the bottom of the form. Figure 7.11 shows my editor as I was building the Auto Spin program.

180

Figure 7.11: The Auto Spin program features two invisible controls and one picture box that is apparent to the user.

The code for the Auto Spin program is almost completely identical to that of Spin Globe. The only difference is that I replaced the button with a timer. The timer control is set to trigger an event at regular intervals. It has two primary properties. The Enabled property turns the timer on and off, and the Interval property describes how many milliseconds (1000ths of a second) between events. If you set the interval to 500, for example, the events occur every half−second.

Trick In game programming, the general rule of thumb is 10 frames per second, so you often see the timer interval set at 100, which generally causes the game’s animations to run at that speed. However, this interval is only a guideline. Even if you set the interval to 1 millisecond, it’s unlikely that the computer will be able to comply because there may be too many calculations for the processor to manage in the given interval. If you specify an interval the computer cannot manage, it will just go as fast as it can. An interval of 100 is more than enough for most simple games and animations and slow enough that it probably won’t bog down the rest of the computer.

Configuring the Timer

To use a timer in your program, drag the timer control onto your form, set the Enabled property to true, and set the interval to whatever speed you want.

Trap Don’t forget to set the timer’s Enabled property to true. The default value is false, which means that the timer will not fire off at all and your carefully planned game or animation will just sit there doing nothing.

After you add a timer, you can add code to its only event—Tick. The Tick event occurs at the intervals you specified with the Interval property. If Interval is 250, any code in the Timer1_Tick() method will happen every 250 milliseconds, which is four times a second. For the Auto Spin program, I set the interval to 100 milliseconds. The code inside Timer1_Tick() is identical to the

181

Соседние файлы в предмете Программирование на C++