I’m very new to object oriented programming in general, and I have a question about how to instance/work with global variables which might be caused by a lack of understanding of Java/OOP in general rather than being specific to Processing. Here I have code where I have instanced global variables which are then incremented randomly by objects of the BubbDraw class. This code worked in the Processing iOS app which I think uses an older version of Processing, however I believe the fact that the variables are instantiated outside of any function is causing an error in Processing 3 on my PC.
I’ve heard that it’s kosher to keep variables within functions, but my question is specifically where to instantiate these variables. I’m not entirely sure whether the variables need to be global, but my only concern is that the variables can’t be instantiated within the drawBubb() function because I don’t want the variables to reset every time the function is called, of course. If I have to create a new function to instantiate these variables, can anyone walk me through how and where to place this function so the variables can be accessed by the drawBubb() function?
//create rgb list
float[] rgb = new float[4];
for (int i = 0; i < 3; i++) {
rgb[i] = random(255)
}
//initialize opacity control
sizeoprange = 20;
rgb[3] = random(sizeoprange);
//initialize ellipse size step variables
sizestep = 1;
sizerange = 20;
//bubble class
class BubbDraw{
void drawBubb() {
//color increment
for (int i = 0; i <3; i++) {
randinc = second()%15
rgb[i] += random(-randinc, randinc);
rgb[i] = constrain(rgb[i], 50, 255);
}
//opacity increment
sizeopstep = 0.2;
sizeoprange += random(-sizeopstep, sizeopstep);
sizeoprange = constrain(sizeoprange, 1, 100);
rgb[3] = random(sizeoprange);
fill(rgb[0], rgb[1], rgb[2], rgb[3]);
//size increment
sizestep = 1;
sizerange += random(-sizestep, sizestep);
sizerange = constrain(sizerange, 1, 40);
sizey = random(sizerange);
ellipse(random(width), random(height), sizey, sizey);
}
}
//number of objects
numbubs = 1000;
//instantiate list of objects
BubbDraw[] bubblist = new BubbDraw[numbubs];
for (int i = 0; i < numbubs; i++) {
bubblist[i] = new BubbDraw();
}
void setup() {
size(displayWidth, displayHeight);
noStroke();
background(0);
}
void draw() {
for (int i = 0; i < numbubs; i++) {
bubblist[i].drawBubb();
}
}