Project file structure for sound03.
As you can see in Figure 2, all of the sound and image files are stored in the folder named src . In addition, all of the sound files were manually copied into the folder named bin .
Will explain in fragments
I will explain the code for this program in fragments. Complete listings of the MXML code and theActionScript code are provided in Listing 10 and Listing 11 near the end of the lesson.
The MXML code
The MXML code is shown in Listing 1 and also in Listing 10 for your convenience.
Mxml code for the project named sound03.
<?xml version="1.0" encoding="utf-8"?><!--
This project is intended to demonstrate the use of soundwith ActionScript. See the file named Driver.as for more
information--><mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>
As is often the case in this series of tutorial lessons, the MXML file is very simple because the program was coded almost entirely in ActionScript. TheMXML code simply instantiates an object of the Driver class. From that point forward, the behavior of the program is controlled byActionScript code.
The ActionScript code
Import directives for the Driver class
The code for the Driver class begins in Listing 2, which shows the package declaration and the import directives.
Import directives for the driver class
package CustomClasses{
import flash.display.Bitmap;import mx.containers.Canvas;
import mx.controls.Image;import mx.events.FlexEvent;
import flash.events.TimerEvent;import flash.utils.Timer;
import flash.media.Sound;import flash.net.URLRequest;
import flash.media.SoundChannel;import flash.events.Event;
New to this lesson
The directives to import the Sound class, the URLRequest class, the SoundChannel class, and possibly the Event class are all new to this lesson.
Beginning of the Driver class proper
The definition of the Driver class begins in Listing 3.
Beginning of the driver class proper.
public class Driver extends Canvas {
//Extending Canvas makes it possible to locate// images with absolute coordinates. The default
// location is 0,0;private var smallSky:Image = new Image();//Instantiate a Timer object that will fire ten events
// per second.private var timer:Timer = new Timer(100);//Declare a counter that will keep track of the number
// of timer events that have been fired.private var loopCntr:uint;//Declare variables for the four sounds.
private var sizzle:Sound;private var thunder:Sound;
private var wind:Sound;private var rain:Sound;//Declare variables that are used to control when the
// thunder sound is played.private var channel:SoundChannel;
private var sizzlePlaying:Boolean = false;
Nothing new here
There is nothing new in Listing 3. I will call your attention to the declaration of variables of type Sound and SoundChannel . Otherwise, no explanation beyond the embedded comments should be required.
The constructor for the Driver class