image names in an array
in
Programming Questions
•
2 years ago
I have an image array question that should be not impossible.. but the solution is escaping me today. When you have a set of images for an array and they all end in a sequential #... how do express this in the String? So say my images are called ball1.jpg ... ball10.jpg. Getting a NullPointerException for this line: " String imageName = "ball" + nf(i, 2) + ".jpg";" My number formating is 2 becuase 10 is two digit (?)
This is an example taken from the blue book..
int numFrames = 10; // The number of animation frames
PImage[] images = new PImage[numFrames]; // Image array
void setup() {
size(200,200);
frameRate(30); // Maximum 30 frames per second
// Automate the image loading procedure. Numbers less than 100
// need an extra zero added to fit the names of the files.
for (int i = 0; i < images.length; i++) {
// Construct the name of the image to load
String imageName = "ball" + nf(i, 2) + ".jpg";
images[i] = loadImage(imageName);
}
}
void draw() {
// Calculate the frame to display, use % to cycle through frames
int frame = frameCount % numFrames;
image(images[frame], 0, 0);
}
1