Problem with my first Object ...
in
Programming Questions
•
2 years ago
I am an english literature student teaching myself computer programming and this processing language seems like a lovely place to start. I am roughly 120 pages through Learning Processing , which is very well written, and I have the following problem. This is also my first post to the forum ... so hello ...
I would like to know why my first function "jiggle" is only applied to the first instance of "square" on the far left side. Why isn't my function "jiggle" applied to every instance of "square" in the following code? Thank you so much in advance if you can assist.
Square[] squares = new Square[10];
void setup() {
size(500,500);
smooth();
for (int i = 0; i < 10; i++) {
squares[i] = new Square(i*50,0,50,500);
}
}
void draw() {
background(255);
for (int i = 0; i < 10; i++) {
squares[i].jiggle();
squares[i].display();
}
}
class Square {
float x;
float y;
float w;
float h;
Square(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void display() {
rect(x, y, w, h);
}
void jiggle() {
if (mouseX > x && mouseX < w && mouseY > y && mouseY < h) {
fill(255, 0, 0);
x = x + random(-1, 1);
y = y + random(-1, 1);
}
else {
fill(0, 255, 0);
}
}
}
I would like to know why my first function "jiggle" is only applied to the first instance of "square" on the far left side. Why isn't my function "jiggle" applied to every instance of "square" in the following code? Thank you so much in advance if you can assist.
Square[] squares = new Square[10];
void setup() {
size(500,500);
smooth();
for (int i = 0; i < 10; i++) {
squares[i] = new Square(i*50,0,50,500);
}
}
void draw() {
background(255);
for (int i = 0; i < 10; i++) {
squares[i].jiggle();
squares[i].display();
}
}
class Square {
float x;
float y;
float w;
float h;
Square(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void display() {
rect(x, y, w, h);
}
void jiggle() {
if (mouseX > x && mouseX < w && mouseY > y && mouseY < h) {
fill(255, 0, 0);
x = x + random(-1, 1);
y = y + random(-1, 1);
}
else {
fill(0, 255, 0);
}
}
}
1