Howdy!
I want to create a sequence of events as determined by an array, but for some reason, only the last element of the array is accounted for.
Also, I would like the actions to occur with a slight delay with respect to each other. The delay() function would not work as it would stop all the program, while I would need the program to keep on running during the delay.
Below is some code to make a particular color square according to the integer stored in an array. The only square that seems to appear is the one specific to the last element of the array.
Ideally, what I would want is for every square, as determined by each element in the array, to appear sequentially, with something like a one second delay between each square’s apparition. The simplified sketch below could employ the delay() function, but I made it as an illustration of a problem in a more complex sketch, in which they delay actions should occur without interfering with all other aspects of the program running in tandem.
int[] array = new int[5];
void setup() {
size(600, 600);
background(128);
for (int i = 0; i < array.length; i++) {
array[i] = int(random(4));
}
println(array);
}
void draw() {
//background(128);
}
void keyPressed() {
for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
fill(0);
rect(200, 200, 100, 100);
}
if (array[i] == 1) {
delay(300);
fill(255, 0, 0);
rect(200, 200, 100, 100);
}
if (array[i] == 2) {
fill(0, 255, 0);
rect(200, 200, 100, 100);
}
if (array[i] == 3) {
fill(0, 0, 255);
rect(200, 200, 100, 100);
}
if (array[i] == 4) {
fill(255);
rect(200, 200, 100, 100);
}
}
}
Thanks for any help.