5c – Animation: an array of Images

What is an array? – Tutorial here.
Declare and initialize and array – See here
How to use an array to animate images – see here

The example we worked up in class is here. For it to run, you will need to set up a sketch with images labelled cat0.jpg, cat1.jpg, cat2.jpg and cat3.jpg and have them in the data folder of your sketch.

//ARRAY OF IMAGES
//declare data type 
PImage[] images = new PImage[4];  //Array of Pimages - 4 spots long
int j=0;// a variable to call each position in our array
int n=10;
 
void setup(){
  size(300,300);
  //fill our array
 for(int i=0;i<4;i++){ //run through values of i from 0-4
  images[i] = loadImage("cat"+i+".jpg"); //use these values to fill each box in our array
}
}
void draw(){
  //image function
  //image(data, x,y,width,height);
 
  if(j>3){ //if i reach the end of my array
    j=0; //send j back to zero
  }
 
    image(images[j],0,0, width,height); //draw images to screen for each value of j (position in your array)
 
   j++; //j generates values that we then use to call each position of the array
   println(frameRate);
 
}