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

Trick You can get good image sequences for animation in many ways. If you are a talented artist, you yourself can generate them. If your artistic skills are not so good (mine aren’t), you can often overcome innate lack of talent with time, technique, and good software. I used an open−source image program named The Gimp to generate the images for this program (and, in fact, every image and screen shot in this book). I used a special script named Spinning Globe, which automatically maps any image onto a globe and creates several frames perfect for animating. A copy of The Gimp is available on the CD−ROM that accompanies this book. It features many other very powerful animation scripts and a host of other features. Of course, you can also use any high−end image−editing program you want. I recommend that your image software have support for transparency, layers, and color manipulation tools. A person with even a modest drawing ability can do good work using such tools.

Displaying an Image from the Image List

Almost all the code for the Spin Globe program is in the click event of the button. I added one class−level variable named counter:

private int counter = 0;

The counter variable is used to determine which frame of the animation should be displayed. I declared it outside any methods but inside the class. I’ll explain in a moment why I did that, but take a look at the code for the button click first:

private void btnNext_Click(object sender, System.EventArgs e) { counter++;

if (counter >= 10){ counter = 0;

} // end if

picGlobe.Image = myPics.Images[counter]; } // end btnNext_Click

Each time the button is clicked, the value of the counter variable is incremented by 1 with the counter++ statement. The value of counter relates to the indices of the images in the ImageList. If you refer to Figure 7.9, you’ll see that the images are numbered from 0 to 9. I carefully set up counter so that it would count from 0 to 9 as well. It’s important to notice that the counter variable was declared inside the class but not inside the method. If counter was declared inside the method, it would be reinitialized each time the button is clicked, and its value would never get beyond 1. Because counter was declared at the class level, it keeps its value while the class is running, and it can be accessed multiple times from the method without problems.

Trap If you’ve programmed in older (non .NET) versions of Visual Basic, you might remember the static keyword that was often used to refer to exactly this kind of variable. C# supports the static keyword as a variable modifier, but this does not work in the same way. It’s better to declare a variable at the class level if you will need it in more than one method or if the same method will need to keep track of its value across multiple calls.

The critical part of the button_click code is the line that copies myPics.Images [counter] to the Image property of the picture box. It takes the element from the image list with the same index as counter and displays that image in the picture box.

179

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