setup()
setup() runs once when the sketch starts, making it the ideal place to size the canvas and initialize arrays of objects like the bubbles here.
🔬 This loop fills the bubbles array. What happens visually if numBubbles is changed to 100? What if it's changed to 2?
for (let i = 0; i < numBubbles; i++) {
bubbles.push(new Bubble());
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Set initial fish position to the center of the canvas
fishX = width / 2;
fishY = height / 2;
// Create bubble objects and add them to the array
for (let i = 0; i < numBubbles; i++) {
bubbles.push(new Bubble());
}
// Initialize lastPhraseChangeTime
lastPhraseChangeTime = millis();
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
for (let i = 0; i < numBubbles; i++) {
bubbles.push(new Bubble());
}
Creates numBubbles new Bubble objects and adds them to the bubbles array
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window.
fishX = width / 2;- Places the fish horizontally at the center of the canvas.
fishY = height / 2;- Places the fish vertically at the center of the canvas (this gets overridden each frame in draw()).
for (let i = 0; i < numBubbles; i++) { bubbles.push(new Bubble()); }- Loops numBubbles times, creating a new Bubble object each time and adding it to the bubbles array.
lastPhraseChangeTime = millis();- Records the current time in milliseconds so the speech bubble timer has a starting point.