The constructor for the Driver class is shown in its entirety in Listing 4.
The constructor for the driver class.
public function Driver(){//constructor
//Load the sky image.//Note the use of a / to eliminate the "Unable to
// resolve asset for transcoding" Compiler Error[Embed("/smallsky.jpg")]
var imgSmall:Class;smallSky.load(imgSmall);//Load four sound files and play two of them now.
sizzle = new Sound();sizzle.load(new URLRequest("sizzle.mp3"));thunder = new Sound();thunder.load(new URLRequest("thunder.mp3"));wind = new Sound();
wind.load(new URLRequest("wind.mp3"));//Play the wind sound through twice at startup.
wind.play(0,2);rain = new Sound();rain.load(new URLRequest("rain.mp3"));
//Play the rain sound foreverrain.play(0,int.MAX_VALUE);//Register an event listener on the CREATION_
// COMPLETE event.this.addEventListener(FlexEvent.CREATION_COMPLETE,
creationCompleteHandler);} //end constructor
There are quite a few things in Listing 4 that are new to this lesson.
Embed the image file
Although the code required to embed the image file in the swf file is not new to this lesson, it is worth highlighting the need to include the slash characterto make the code compatible with the FlashDevelop IDE.
Load the sizzle sound
Listing 4 instantiates a new Sound object and stores the object'sreference in the instance variable named sizzle . Then it calls the load method on that object to load the contents of the sound file named sizzle.mp3 into the new Sound object.
The load method of the Sound class
Here is part of what the documentation has to say about the load method of the class named Sound :
"Initiates loading of an external MP3 file from the specified URL. If you provide a valid URLRequest object to the Sound constructor, theconstructor calls Sound.load() for you. You only need to call Sound.load() yourself if you don't pass a valid URLRequest object to the Sound constructor oryou pass a null value.
Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file,create a new Sound object."
Because I didn't provide a URLRequest object to the constructor when I instantiated the object of the Sound class, it was necessary for me to call the load method on the Sound object to load the sound file named sizzle.mp3 .
Required parameter for the load method
Only one parameter is required by the load method and it must be of type URLRequest . To make a long story short, at least for the case where the sound file is located in the src folder as shown in Figure 2, you can create the required URLRequest object by calling the constructor for the URLRequest class and passing the name of the sound file as a String parameter to the constructor as shown in Listing 4.
Don't play the sizzle sound yet
The sizzle sound and the thunder sound are both encapsulated in Sound objects by the constructor in Listing 4. However, those sounds are not played bythe constructor.