xxxxxxxxxx
53
/* The current sketch shows an array of bubbles. Add two additional features to this sketch using JavaScript splice(). First, bubbles would dissapear on mousePressed. Second, bubbles would reappear at a random position on the screen on mouseReleased. */
let bubbles = [];
let newBubble = [];
let d;
function setup() {
createCanvas(800, 800);
for (let i = 0; i < 20; i++) {
bubbles[i] = new Bubble();
}
}
function draw() {
background(7, 89, 155);
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);
bubbles.splice (i,1,new Bubble());
bubbles.splice (i,1,new Bubble());
}
} 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);
}
}