From a2b6d27ecc4b316f7234f700f2faa46e9d7c7551 Mon Sep 17 00:00:00 2001 From: Michael DiBernardo Date: Mon, 26 Aug 2024 19:08:52 -0400 Subject: [PATCH] Switch to async preloading. --- main.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/main.js b/main.js index 889ae17..169dfef 100644 --- a/main.js +++ b/main.js @@ -98,7 +98,7 @@ function happyDance(bug) { }, bugDanceDuration); } -document.addEventListener("DOMContentLoaded", () => { +async function startGame() { const splashScreen = document.getElementById("splash-screen"); const scrollableArea = document.getElementById("scrollable-area"); const rockTemplate = document.getElementById("rock-template"); @@ -142,19 +142,20 @@ document.addEventListener("DOMContentLoaded", () => { } } - function initialize() { - preloadAssets() - .then(() => { - createRocks(); - }) - .catch((error) => { - console.error("Error preloading assets:", error); - }) - .finally(() => { - // Hide splashscreen regardless of whether prefetch was successful. - hideSplashScreen(); - }); + async function initialize() { + try { + await preloadAssets(); + } catch (error) { + console.error("Error preloading assets:", error); + } finally { + createRocks(); + hideSplashScreen(); + } } initialize(); +} + +document.addEventListener("DOMContentLoaded", () => { + startGame(); });