I have this code to add a new ball to the array but strangely it doesn’t work.
let balls = [];
let num_balls = 1;
let w = 800;
let h = 600;
function setup() {
createCanvas(w, h);
for (let i = 0; i < num_balls; i++) {
balls[i] = new ball(100 * i, 100);
}
}
function draw() {
background(0);
for (let i = 0; i < num_balls; i++) {
balls[i].display();
}
}
function mousePressed() {
let myball = new ball(mouseX, mouseY)
balls.push(myball);
}
class ball {
constructor(tempX, tempY) {
this.x = tempX;
this.y = tempY;
}
display() {
ellipse(this.x, this.y, 100, 100);
}
}
Anything amiss here? Got really puzzled. Thanks in advance.