HTML and CSS Reference
In-Depth Information
document . body . appendChild ( shootSound );
shootSound . addEventListener ( "canplaythrough" , itemLoaded , false
false );
shootSound . setAttribute ( "src" , "shoot1." + audioType );
alienImage = new
new Image ();
alienImage . onload = itemLoaded ;
alienImage . src = "alien.png" ;
playerImage = new
new Image ();
playerImage . onload = itemLoaded ;
playerImage . src = "player.png" ;
missileImage = new
new Image ();
missileImage . onload = itemLoaded ;
missileImage . src = "missile.png" ; appState = STATE_LOADING ;
}
If you recall, STATE_LOADING does nothing in our run() function; it just waits for all events
tooccur.Theactionhereishandledbythe itemLoaded() eventhandler,whichworksexactly
likethe itemLoaded() functionwewrotefortheaudioplayer,exceptthatithastwoaddition-
al functions:
1. It must remove the event listeners from the two sound objects we created. This is be-
cause, in some browsers, calling the play() method of an HTMLAudioElement ob-
ject—or changing the src attribute of an HTMLAudioElement object—initiates a load
operation, which will then call the itemLoaded event handler a second time. This will
cause unexpected results in your application. Furthermore, it is always a good idea to
remove unneeded event handlers from your objects.
2. We set the appState to STATE_RESET , which will initialize the game the next time the
run() function is called on the interval.
Here is the code with the two additional functions:
function
function itemLoaded ( event ) {
loadCount ++ ;
iif ( loadCount >= itemsToLoad ) {
shootSound . removeEventListener ( "canplaythrough" , itemLoaded , false
false );
explodeSound . removeEventListener ( "canplaythrough" , itemLoaded , false
false );
appState = STATE_RESET ;
}
}
Search WWH ::




Custom Search