xxxxxxxxxx
39
// Initializes empty array to hold all Balls
let balls = [];
function setup() {
createCanvas(600, 600);
}
function draw() {
background(0);
// Loops through each ball in balls array
for (let ball of balls) {
ball.move();
ball.bounce();
ball.display();
}
}
function mousePressed() {
createBall(); // Calls createBall function to create new Ball
}
function createBall() {
// Sets new ball's position to current mouse position
let x = mouseX;
let y = mouseY;
// Generates a random speed for new ball in both x and y directions
let xspeed = random(-5, 5);
let yspeed = random(-5, 5);
// Generates random size for new ball
let size = random(10, 50);
// Creates new Ball object with specified parameters and adds it to balls array
balls.push(new Ball(x, y, xspeed, yspeed, size));
}