Array of PImages

Hello,

i am trying to build an array of images to be displayed i looping, but I receive the error message that image cannot be solve as variable.
Here’s code:

int maxImages = 10;
int imageIndex = 0;
PImage[] images = new PImage [maxImages];
void setup() {
  
  size(500, 555);
  
}

void draw(){
 for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "  i + 1 .jpg" );


image(images[imageIndex], 0, 0);
imageIndex = (imageIndex + 1)& image.length;

}
}

Can somebidy guive me a light?

1 PImage[] images = new PImage[maxImages]; 
2 images[i] = loadImage((i + 1) + ".jpg");
3 imageIndex = (imageIndex + 1) % images.length;
4 Move image loading to setup() 





1 Like

Hello @nikapm ,

An important part of your journey in code development is using the tools you have to debug and work through your code.

Almost everything you need is here:
https://processing.org/

Once you learn to navigate these it gets easier over time.

References:
+ (addition) / Reference / Processing.org
setup() / Reference / Processing.org // Runs once!
draw() / Reference / Processing.org // Loops at default framerate of 60 fps

This is not correct (there is no image variable).
If you move mouse over the error (red underline) it shows:

There is no image named " i + 1 .jpg" (everything inside quotes).
Read this reference to see how to add an integer to a string:
+ (addition) / Reference / Processing.org

The imageIndex is i in your for() loop and you do not need a separate variable.

The reference for math operators is here and you will see what the & does:
Reference / Processing.org

You are displaying the images one after another in the for() loop!
Since the sketch window updates at the end of draw cycle you will only see the last image (once the code is corrected).

Use a println() statements to help with debugging throughout your code.
Reference:
println() / Reference / Processing.org

Code example and use of println() to help debugging:

int maxImages = 10;
int imageIndex = 0;

PImage[] images = new PImage [maxImages];
  
for (int i = 0; i < maxImages; i ++ )
  {  
  // Important: Creates a String to use for image names
  String imgNum = str(i); // this could be i+1 if your image names are 1 to 10
  String imgExt = ".jpg";
  String imgName = imgNum + imgExt;
  
  // Shortcut for above:
  //String imgName = i + ".jpg"; // This casts i to a String before adding to a String
  
  println(imgName);
  
  images[i] = loadImage(imgName); 
  // This will give errors since I do not have images.
  // Console will show what is missing and that code used correct name!
  
  imageIndex = (imageIndex + 1) & maxImages; // You are using a bitwise & here!
  println(i, imageIndex);                    // See result of above math that is used in next loop.
  } 

The PImage array indexes from 0 to 9 and you load images (I am assuming) from 1.jpg to 10.jpg.
Add the 1 offset carefully in your code.

Have fun!

:)

1 Like

Should be

imageIndex = (imageIndex + 1 ) % maxImages;

using the modulo % operator rather than bit-wise & operator.

1 Like