Hey, I’m trying to use a for loop to create a grid of objects and getting an out-of-bounds issue. I can’t get int k to increment through the inner loop…actually, when I place it in the outer for loop it increments and produces the objects with the specified amount but it doesn’t create the grid. It will create a single column or row instead.
Thanks,
///////////////////// main//////////////////////
Image [] animations = new Image[4];
void setup() {
size(640, 640);
frameRate(30);
PImage [] seq = new PImage[15];
for (int i = 0; i < seq.length; i++) {
seq[i] = loadImage("anthro/anthro" + nf(i+1, 2) + ".jpg");
seq[i].resize(width/animations.length, height/animations.length);
}
float x = 0;
float y = 0;
int k = 0;
////grid.
for (int i = 0; i<animations.length; i++) {
for (int j = 0; j<animations.length; j++) {
animations[k] = new Image(seq, x+i*seq[0].width, y+j*seq[0].height);
k++;
}
}
}
void draw() {
background(0);
for (int i = 0; i<animations.length; i++) {
animations[i].display();
animations[i].next();
}
}
////////////////class////////////////////////////////
class Image {
PImage[] images;
float x;
float y;
float index = 0;
float speed;
Image(PImage[] images_, float x_, float y_) {
images = images_;
x = x_;
y = y_;
speed = 1;
index = random(0,14);
}
void next() {
index += speed;
if (index>=images.length) {
index -= images.length;
};
}
void display() {
int imageIndex = int(index);
image(images[imageIndex], x, y);
}
void move() {
x += speed;
if (x>=width) {
x = -images[0].width;
}
}
}