Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit) (+1)

Hint #4 -- Audio!

Sound is to games what the frame is to a painting. Even a simple effort makes a huge improvement in the overall feel of the game. And thanks to sites like Open Game Art, or Incompetech, there are a wealth of audio resources available. And, of course, every game author who is not me, is also an acid-house-trance DJ on the side. So make sure you use these resources to bring your games to life!

Oh? What's that? You say Canned Heat doesn't have any audio handling capabilities? Hmmm. I guess I didn't get around to putting those in before the jam started. Let's see if we can fix that. First we need to load some sound:

   (define (mk-sound name path)
     "( name path -- entity) Create audio entity."
     (let ([a (new Audio)])
       (:= a.src path)
       (store (object [tag 'audio] [name name] [au a]))))

This function makes an "audio" entity in the Store. It's pretty simple. It creates a web Audio object, sets the source to whatever file you might have for it and drops it in the Store, ready for use. Most modern browsers can load most any audio file format you're likely to use (although FireFox seems to have some problems with %.wav format for some reason). If you don't have a particular requirement, I would recommend %.ogg for sound effects and %.mp3 for longer pieces, like the orchestral score you  composed in Ableton last night. Here's how to use mk-sound when initializing stuff:

(mk-sound 'ponk "a/Interface Element 3.mp3")
(mk-sound 'spring "a/spring.ogg")
(mk-sound 'pop  "a/Pop.mp3")

I generally try to load it before my image resources. It probably doesn't matter that much, but...

And now, maestro, you need to play those sounds where they'll do the most good. Maybe in a key handler when the player presses the jump key, or perhaps in the collision detection System when they run into something. To do that, you'll need a way to play them:

   (define (play name)
     "( name --) Play named audio entity."
     (let ([a (grab (λ (e) (and (= e.tag 'audio)
                                (= e.name name))))])
       (when a
         (:= a.au.currentTime 0)
         (a.au.play))))

With this function, you can just say "(play 'spring)" and the heady sound of coiled metal will grace your ears!

Oh! One quick note -- sometimes browsers will be reluctant to play sounds without user interaction. This is a defense against all those awful auto-playing adverts that were popular a while back. Haven't ever seen that? Thank your browser for protecting you. However, it does mean that you may not hear any sound after all the trouble you took to retrofit your game. Fortunately, the solution is (fairly) simple. You can tell the browser to allow auto-playing media on the game page, or... You can just have the user do something on the page, which usually unblocks the audio. This can be as simple as hitting a key or clicking a button. When you publish your web game on itch.io, if you set it for "click to play", then that initial click will do that for you.

Make mine music!