I made some simple test code for setting up and using an array of colours that worked fine as a straight through thing (i.e. no setup, no draw). Then I modified it for use with a setup and frame loop and eventually got it to work, but I am confused about what went wrong.
It didn’t work to declare the array as a global variable, and try to initialise the values outside of setup and draw:
int palettesize = 3;
color[] palette = new color [palettesize]; // I get an error flagged that it wants a left curly
palette[0] = color(209, 54, 39, 25);
palette[1] = color(209, 54, 39, 100);
palette[2] = color(209, 54, 39, 200);
It didn’t work to declare the whole thing inside of setup as then the variable wasn’t possible to reference from within draw. What did work was to set up the empty array outside of setup and draw, and fill its values within setup, like so:
int palettesize = 3;
int howtall = 20;
color[] palette = new color [palettesize];
void setup() {
frameRate(1);
palette[0] = color(209, 54, 39, 25);
palette[1] = color(209, 54, 39, 100);
palette[2] = color(209, 54, 39, 200);
}
void draw() {
if (frameCount < palettesize+1) {
color usecolor = palette[frameCount-1];
fill(usecolor);
rect(0,frameCount*howtall, 10, howtall);
}
}
Can someone explain to me what’s going on here? What is puzzling is why I can’t initialise the array values outside of the setup loop, when I can initialise an integer variable outside the setup loop, and, in fact need to do so.
Thanks!
(Not familiar with Java except via learning Processing right now - but have used a variety of other languages)