HTML and CSS Reference
In-Depth Information
explodeSound2
.
setAttribute
(
"src"
,
"explode1."
+
audioType
);
explodeSound3
=
document
.
createElement
(
"audio"
);
document
.
body
.
appendChild
(
explodeSound3
);
explodeSound3
.
addEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
explodeSound3
.
setAttribute
(
"src"
,
"explode1."
+
audioType
);
shootSound
=
document
.
createElement
(
"audio"
);
document
.
body
.
appendChild
(
shootSound
);
shootSound
.
addEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
shootSound
.
setAttribute
(
"src"
,
"shoot1."
+
audioType
);
shootSound2
=
document
.
createElement
(
"audio"
);
document
.
body
.
appendChild
(
shootSound2
);
shootSound2
.
addEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
shootSound2
.
setAttribute
(
"src"
,
"shoot1."
+
audioType
);
shootSound3
=
document
.
createElement
(
"audio"
);
document
.
body
.
appendChild
(
shootSound3
);
shootSound3
.
addEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
shootSound3
.
setAttribute
(
"src"
,
"shoot1."
+
audioType
);
In the
itemLoaded()
function, we remove the event listeners for all six loaded sounds:
shootSound
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
shootSound2
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
shootSound3
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
explodeSound
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
explodeSound2
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
explodeSound3
.
removeEventListener
(
"canplaythrough"
,
itemLoaded
,
false
false
);
Then we push each sound into our
soundPool
array. However, this time, we push them as
dynamic objects so that we can set the following properties, which don't exist in the
HTMLAu-
dioElement
object:
name
The name of the sound file to play (again, without the extension).
element
The reference to the
HTMLAudioElement
object.
played
A Boolean that tells us whether this sound has played once or not. We need this property
because we are putting all of these
sound
objects into our array, but they have not been