preload()
preload() runs before setup() and guarantees all external assets (fonts, images) finish loading. Without it, images would be null when draw() tries to render them, causing blank or broken visuals. Always load media here.
function preload() {
uiFont = loadFont(
"https://cdn.jsdelivr.net/fontsource/fonts/roboto@latest/latin-400-normal.woff"
);
// Main sprite image (the monkey face)
monkeyImg = loadImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRbl5eODr1SJybpcuw7MsYEbPVUhhVmITXjng&s"
);
// First crash screen image (fades in after collision)
crashImg = loadImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTgtFQUdSVRY_u5I8be9kJcbHGia5PLCg3OVQ&s"
);
// Second crash screen image (fades in after 2 more minutes)
crashImg2 = loadImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRKtsduK6_gd19NEyj2MizOpjz2ZaPHYrvT2g&s"
);
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
uiFont = loadFont(...)
Loads Roboto font from CDN for HUD text display
monkeyImg = loadImage(...)
Loads the main sprite image that will be drawn as a 3D billboard
crashImg = loadImage(...)
Loads the glitch image that fades in during Phase 1 of the crash sequence
crashImg2 = loadImage(...)
Loads the second glitch image that fades in during Phase 2 of the crash sequence
uiFont = loadFont(...)- Fetches the Roboto font file from a CDN and stores it; preload() guarantees it finishes loading before setup() runs
monkeyImg = loadImage(...)- Downloads the monkey sprite image from the URL and stores it in a variable so display3D() can texture it onto a plane later
crashImg = loadImage(...)- Pre-downloads the first crash screen image so it's ready to fade in after collision without delay
crashImg2 = loadImage(...)- Pre-downloads the second crash screen image to display during Phase 2 of the crash transition