๐ Audio Mechanics Lab
Sound makes a tiny game feel finished. Learn a few reusable patterns โ then add them anywhere.
1 ยท Put the library in your game folder
Your folder might look like this:
my-game/
index.html
rbs-game-library.js
rbs-audio-library.js
sounds/
collect.mp3
hit.mp3
win.mp3
music.mp3Then, just before </body>:
<script src="rbs-game-library.js"></script>
<script src="rbs-audio-library.js"></script>
<script>
// YOUR GAME CODE HERE
</script>Make a sound
let collectSound = makeSound(
"sounds/collect.mp3",
0.7
);0 = silent. 1 = full volume.
Play it
if (touching(player, coin)) {
playSound(collectSound);
}Perfect for collect, jump, hit, unlock and win sounds.
Allow overlap
playOverlap(
"sounds/laser.mp3",
0.5
);Useful when the same effect may happen again before the previous sound finishes.
Background loop
let music = makeSound(
"sounds/music.mp3",
0.25,
true
);
playSound(music);Keep music much quieter than effects.
Stop / pause / volume
pauseSound(music); stopSound(music); setVolume(music, 0.15);
Fade out
fadeOut(music);
Useful at GAME OVER or when changing screens.
Important browser rule
Browsers often block sound that starts automatically before the player has interacted with the page. The safest pattern is: player clicks START โ start music. Sound effects triggered after keyboard, mouse or gamepad interaction are usually much easier.
Sound + haptic = powerful feedback
if (touching(player, gem)) {
score = changeNumber(score, 1, scoreText);
playSound(collectSound);
rumble(0.12, 0.25, 80);
randomPosition(gem, game);
}Think in feedback recipes:
Collect โ bright sound + tiny buzz ยท Hit โ heavy sound + strong rumble ยท Win โ success sound + longer rumble.
Challenge
Add three sounds to your game: one action sound, one danger sound and one win sound. Then decide which one should also use haptics.