Macromedia Flash MX 2004 ActionScript Programming Tutorial

The Sound Object

Our game wouldn’t be complete without sounds of shooting and exploding. To add these, we’re going to use the Sound object. The Sound object uses sounds attached from the library, much as we just attached a movie clip from the library.

If you’ve ever added sound to frames in a movie, you probably know that you can use them to control playback; you can’t do that using the Sound object, but the Sound object does provide some advantages. Like any object, it has built-in methods that you can use for loading a sound, removing the sound when it’s no longer needed, applying sound transforms, and controlling the volume and offset of sound output (panning).

To give you a simple idea of how to use the Sound object, we’ll load a laser sound into our laser movie clip, so whenever the user fires the gun, it makes a shot-like sound:

  1. If necessary, import the sound you’re going to use to the library.
  2. Right-click on the sound, select Linkage, and, in the Linkage dialog, give the sound a unique linkage ID. You’re doing the same thing you did when attaching a movie clip—giving the sound an identifier you can use to attach the sound in the movie.

  1. Next, we need to create a new Sound object and attach the sound to it. Open the laser_clip movie clip. With the laser movie clip selected inside it, add the following onClipEvent(load) event handler, above the onClipEvent(enterFrame) handler:

onClipEvent(load) {

laserSound = new Sound();

laserSound.attachSound(“laser_fire”);

}

The first line simply creates a new Sound object called “laserSound”. We can then use “laserSound” to access the properties and methods of the Sound object. The second line attaches a specific sound—”laser_fire”—from the library to the laserSound object. Notice that the attachSound method is very similar to the attachMovie method of the MovieClip object. The only argument is the linkage ID of the library sound being attached.

  1. Finally, we play the sound using the start method, which we can add to the end of our onClipEvent(load) handler:

laserSound.start(0,0);

The start method has 2 arguments. The first tells Flash how many seconds into the sound to start playing. We want all the sound to play, so we used 0. The second specifies how many times the sound should loop. Again, we used 0, because we only want a single shot to sound each time the laser is fired.

The onClipEvent(load) event handler should now look like this:

onClipEvent(load) {

laserSound = new Sound();

laserSound.attachSound(“laser_fire”);

laserSound.start(0,0);

}

We included the start method in the onClipEvent(load) handler so the sound will play each time the movie clip is loaded. The movie clip will be loaded each time the user fires the gun.

If you want, you can create movie clips that function as volume and balance controls. You’d simply set the sound object’s volume and balance equal to the position of the appropriate movie clip using the Sound object’s setVolume and setPan methods. Each of these methods has only 1 parameter—volume and pan, respectively. Volume is specified by a value between 1 and 100, with a default being 100, the highest value. Pan is specified by a value between -100 and 100, with -100 representing output to only the left channel and 100 representing output to only the right channel.

To stop a sound from playing, use the stop method:

laserSound.stop();

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you learned about:

  • The MovieClip Object
  • Duplicating move clips
  • Using the setInterval() function
  • Removing duplicated movie clips
  • Using the hitTest method of the MovieClip Object
  • Attaching movie clips
  • Using onMovieClip
  • The Sound Object

One Comment

  1. zoe

Leave a Reply

Your email address will not be published. Required fields are marked *