xxxxxxxxxx
51
let bubbles = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// display and move bubbles
for (let i=0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].display();
}
// cleanup unneeded bubbles
for (let i=bubbles.length-1; i >= 0; i--) {
if (bubbles[i].isDone == true) {
console.log("Removing bubble " + i);
bubbles.splice(i, 1);
}
}
//console.log("We have " + bubbles.length + " in our array");
}
function mousePressed() {
let b = new Bubble(mouseX, mouseY);
bubbles.push(b);
}
class Bubble {
constructor(x, y) {
this.x = x;
this.y = y;
this.isDone = false;
}
move() {
this.y = this.y - 1;
this.x = this.x + random(-2, 2);
if (this.y < -12.5) {
this.isDone = true;
}
}
display() {
ellipse(this.x, this.y, 25, 25);
}
}