Gameplay in HTML5: Homework #8
Create a library module Audio.js with general routines for handling Web audio.
- Write an initialization function that determines the best format (i.e., file extension) using Modernizr. (As you write other parts of this library, you will need to initialize a couple other data structures here, too.)
- Each audio file will be identified by its name, sans extension. (You will probably want to keep these files in a subdirectory,
audio/, say.) There may be multiple
Audio instances of each file. Create a data structure that keeps track of all of the
Audio instances for each file name.
- Write a function
getAudio() that takes a name and returns an
Audio object. It should work as follows: first go through the list of
Audio objects already created for that file and if one has finished playing (ended), return that object. Otherwise create a new
- Maintain a list of sounds that are playing. Set up an event handler for "ended" on each
Audio object to keep that list up to date.
- Write a function to play sounds that takes different actions depending on its argument:
- If the argument is a string, pass it to your
getAudio() to get an
Audio object and play it.
- If the argument is an object with a
name property, do the same with the value of that property, but also check whether the object has a
- Otherwise, the object should have a
names property, whose value is a list of strings, and a
type property. If the
type is, say, "randomSelection", choose one of the names from the list and play it.
- If the
type is "sequence", call a function that sets things up to play all of the files in the
names list. You might want to accept an option (passed as a property of the object) to shuffle the array. You should also have an option to loop back to the beginning or to stop when the sequence is finished.
- Write a function to stop all of the sounds that are playing.
- What functions from this module need to be public?
Obtain some music and sound-effects files. Good sources include
You can edit audio files and convert between formats using Audacity.
Create a module for audio specific to your game, e.g., FreeCellAudio.js. Set up a map that uses game event designations (e.g., "jazz_music", "move_card") as keys; and as values, strings or objects that you can pass to your library to play. Then write a function to play any of these by name.
Place calls in your game code to play music and sound effects using the above module when the triggering events occur. (E.g., when the player wins, play a "win" sound.) When the app switches screens, it should probably stop all sounds.
- Archive your files. E.g.:
tar -czvf Gameplay_8_MyName.tgz directory
(Please do not include jquery.js or modernizr.js files in the archive. Links are OK.)
E-mail the archive to the instructor.