HTML and CSS Reference
In-Depth Information
hear the sound again. This is why we don't save the source after we start playing it. When the
sound is done playing, the browser will garbage collect the reference for us. There is nothing
else we need to do.
function
function
playSound
(
soundBuffer
) {
var
var
source
=
audioContext
.
createBufferSource
();
source
.
buffer
=
soundBuffer
;
source
.
connect
(
audioContext
.
destination
);
source
.
noteOn
(
0
);
}
One last thing: we have updated the game to fire automatically. To accomplish this, we added
two variables:
shootWaitFrames
(the amount to wait before we shoot) and
shootWait
, a
counter that keeps track of how long we have waited before we shoot.
var
var
shootWaitedFrames
=
8
;
var
var
shootWait
=
8
;
We also added the following code to
drawScreen()
to accomplish this. This code makes
the game wait for eight calls to
drawScreen()
before the player fires a shot. You can alter
shootWaitFrames
to make the player fire more or less often. By making this value less, you
can really put the Web Audio API through its paces, with more sounds playing more often.
shootWaitedFrames
++
;
iif
(
shootWaitedFrames
>
shootWait
) {
shoot
();
shootWaitedFrames
=
0
;;
}
NOTE
Google Chrome was the only web browser that supported this example at the time of publication.
Also, the
noteOn()
function will soon be deprecated for
start()
.
You can try this new version of
Space Raiders
by loading
CH7EX10.html
from the code dis-
tribution. Here are some things to note when you try the example:
1. You need to run this example from a web server; otherwise,
XMLHttpRequest()
might
not work.
2. You might need to add MIME types for audio files to your web server configuration if
they will not load.