Search code examples
imagealgorithmanimationprocessingframe-rate

How to end animation? (processing)


I've written this code which creates a sketchbook.

I'm sure that there's a simple error, but why won't it stop playing at the end of the images?

Here is the code

    import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer sou; //variable name;


final int NUMBER_IMAGES = 27; 
PImage[] images; //sets PImage array
int framerate = 10;
int currentImage = 0;
String getImageName(int image_number) {
    if (image_number == 0) {  // == double equals checks for equality
        return "title.gif";  //missing k0.gif and k26.gif until this line   //added
    } else if (image_number == 26) {
        return "title2.gif";
    } else {
      return "data/K" + image_number + ".gif";
    }
}
void setup () {
  minim = new Minim(this); //define construction
  sou = minim.loadFile("ambience.mp3");
  sou.loop();


  size (300, 300);
  background (255); 
  frameRate(framerate);
  imageMode (CENTER);  // Tells the images to display relative to CENTRE 
  images = new PImage[NUMBER_IMAGES];  // initialises the array (not images)

  for (int image_number = 0; image_number < NUMBER_IMAGES; image_number++) {
    String filename;  // Declared a String called filename
    filename = getImageName(image_number);
    images[image_number] = loadImage(filename);
  } 

}
void draw () {
    // Set framerate
    frameRate(framerate);
    // Draws first image
    image(images[currentImage], width/2.0, height/2.0);
    currentImage++;
    currentImage = currentImage % NUMBER_IMAGES;

}
void keyPressed() {
  if (keyCode == UP) { // up arrow increases frame rate by one
    framerate ++;
  }
  if (keyCode == DOWN) { //down arrow decreases framerate by one
    framerate --;
  }
}

I can't think of more details to add although I'm being told I can't post this as it is mostly code.


Solution

  • This line is the one that achieves the recurrent number loop.

    currentImage = currentImage % NUMBER_IMAGES
    

    What the % (Modulo) operator does is to calculates the remainder when one number is divided by another. So lets say for example that your NUMBER_IMAGES is 10, at first you'll have 1 & 10 and the value stored in currentImage would be 1. This continues until you reach 10 % 10 the value stored would be 0 and there you'll start all over again.

    Here you can find more about the (Module) in Processing: https://www.processing.org/reference/modulo.html

    Perhaps a more simple approach to achieve what you'll looking for would be to add a condition to stop when you reach the number of images.

    void draw () {
        // Set framerate
        frameRate(framerate);
        // Draws images
        image(images[currentImage], width/2.0, height/2.0);
        if(currentImage < NUMBER_IMAGES){         
          currentImage++;
        }   
    }
    

    Hope this helps. Regards Jose