Using an Array for Nicer Code
This code's kind of long and unwieldy, with all the 'if' statements. A much nicer way is to use an array. Arrays are variables that can store more than one thing, like a list of numbers, a list of names, or a list of images. You can use a second variable to pick different items out of the array.
An array variable is created by putting two square brackets in the declaration. You then say how many items you want in the arary, and then you can store and retrieve individual items by number.
note - there are 5 numbers in the array. the first one is item 0, and the last is item 4. arrays always start with 0
int[] mylist; // mylist is an array of integers
mylist = new int[5];
mylist[0] = 15;
mylist[1] = 2;
mylist[2] = -3;
mylist[3] = 8;
mylist[4] = 12;
println (mylist); // prints out all the numbers in the array
println (mylist[0] + mylist[1]); // 15+2 = 17
Now we can make the image sequence code a little shorter by using a single array to hold all the images.
(Same set of images as the last example)
/* Image Sequence With Array */
PImage[] hands;
int counter=0;
void setup()
{
size(640,480);
hands = new PImage[6];
hands[0] = loadImage("zero.jpg");
hands[1] = loadImage("one.jpg");
hands[2] = loadImage("two.jpg");
hands[3] = loadImage("three.jpg");
hands[4] = loadImage("four.jpg");
hands[5] = loadImage("five.jpg");
}
void mousePressed()
{
counter++;
if (counter>5)
counter=0;
}
void draw()
{
image(hands[counter],0,0);
}





