xxxxxxxxxx
46
/* the current sketch draws bubbles on the screen using an array of objects. help adding a feature so that when mouse click on a circle the object would be removed from the array using JavaScript splice() */
let bubbles = [];
let d;
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 5; i++) {
bubbles[i] = new Bubble();
}
}
function draw() {
background(220);
for (let i = 0; i < bubbles.length; i++) {
d = dist(mouseX, mouseY, bubbles[i].x, bubbles[i].y);
if (d < 10) {
fill(255);
if (mouseIsPressed){
bubbles.splice(i,1);
}
} else {
fill(255);
}
if (bubbles[i] != null){
bubbles[i].show();
}
}
}
class Bubble {
constructor() {
this.x = random(width);
this.y = random(height);
}
show() {
ellipse(this.x, this.y, 20, 20);
}
}