@glv I made changes you suggested. It’s indeed more clean. It allowed me to see where I made typing mistakes. It works fine now
@Chrisir I tried to remove this.color in the constructor, but it gave a mistake. I look into it further with @GoToLoop suggested documentation (I’m already a fan of Dan Shiffman).
Here is my new code :
let bubble1;
let bubble2;
let bubble3;
let bubble4;
function setup() {
createCanvas(400, 400);
bubble1 = new Bubble(100, 200, 100);
bubble2 = new Bubble(100, 200, 50);
bubble3 = new Bubble(100, 200, 30);
bubble4 = new Bubble(100, 100, 10);
}
function draw() {
background(220);
bubble1.show();
bubble2.show();
bubble3.show();
bubble4.show();
}
function mousePressed() {
b1 = bubble1.inside(mouseX, mouseY);
b2 = bubble2.inside(mouseX, mouseY);
b3 = bubble3.inside(mouseX, mouseY);
b4 = bubble4.inside(mouseX, mouseY);
if (b4 | (b4 & (b3 | b2 | b1))) {
bubble4.clicked();
print("b4");
} else if (b3 | (b3 & (b2 | b1))) {
bubble3.clicked();
print("b3");
} else if (b2 | (b2 & b1)) {
bubble2.clicked();
print("b2");
} else {
bubble1.clicked();
print("b1");
}
}
class Bubble {
constructor(tempX, tempY, tempR) {
this.x = tempX;
this.y = tempY;
this.r = tempR;
this.color = 100;
}
inside() {
let d = dist(this.x, this.y, mouseX, mouseY);
if (d < this.r/2) {
return true;
} else {
return false;
}
}
clicked() {
this.color = (0);
this.x = this.x + 50;
}
show() {
stroke(255);
fill(this.color);
ellipse(this.x, this.y, this.r);
}
}
I tried. I removed this.color from the constructor.
Then I wrote
Clicked() {
color = 0;
thix x = this.x + 50;
}
Color doesn’t change anymore.
To initially build my Bubble class, I followed Dan Shiffman “what it means to be a bubble” https://youtu.be/T-HGdc8L-7w. He says in the Class, always use “this.”.
I read and watched @gotoloop suggested (loved the this dot dance!), and it points in the same direction. I have to use “this.” in the class (both in the constructor and elsewhere in the class).