diff game/js/audio.js @ 6:910b26feca76

full game flow
author ark
date 2026-10-19 01:05 +0200
parents 2d46729a022e
children 2c4fed8baed9
line wrap: on
line diff
--- a/game/js/audio.js	Sun Oct 18 22:50:24 2026 +0200
+++ b/game/js/audio.js	Mon Oct 19 01:05:29 2026 +0200
@@ -11,6 +11,7 @@
     var buffers = {};   // event -> decoded recordings
     var last = {};      // event -> audio clock time of its last play
     var ends = {};      // event -> audio clock time when its sounds have ended
+    var started = false;
 
     // create the audio context and decode the recordings. the browser keeps
     // the context suspended until unlock() runs in a user interaction.
@@ -37,9 +38,16 @@
             function () { console.error("sound not decoded:", name); });
     }
 
-    // call from a pointer or touch handler: browsers start sound only there
+    // call from the play button's handler. sound stays off until then.
+    function start() {
+        started = true;
+        unlock();
+    }
+
+    // call from a pointer or touch handler: browsers start sound only there.
+    // does nothing before start().
     function unlock() {
-        if (!ctx || ctx.state === "running") return;
+        if (!ctx || !started || ctx.state === "running") return;
         ctx.resume();
         // older safari also needs a sound started in the handler
         var src = ctx.createBufferSource();
@@ -100,5 +108,5 @@
         ends[name] = Math.max(ends[name] || 0, when + length);
     }
 
-    return { init: init, unlock: unlock, play: play };
+    return { init: init, start: start, unlock: unlock, play: play };
 })();