O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.1. Creating an Object to Control Sound

Problem

You want to control sounds at runtime.

Solution

Create a new movie clip to serve as the sound’s target and then pass a reference to that clip to the Sound( ) constructor function (to create a new Sound object). Alternatively, create and invoke a custom createNewSound( ) method that handles this process automatically.

Discussion

Sound objects enable you to load sounds, play sounds, and control the volume and panning of sounds within your Flash movies. A Sound object must target a movie clip (_root is also a valid target). The audio that is controlled must be placed in the movie clip’s timeline at authoring time or loaded into the movie clip at runtime using attachSound( ) or loadSound( ). In any case, it is possible for you to place/load multiple sounds into a single movie clip, but doing so is discouraged. The reason for this is that a Sound object controls the audio in the target movie clip as a whole, so it is not possible to control each of the sounds separately if they are all in the same movie clip. Therefore, it is better to place/load each sound into its own movie clip and create a separate Sound object to control each one.

When you create a new Sound object, specify a target movie clip. To control sounds in the current timeline, specify the keyword this as the target.

// Incorrect. This does not work properly. mySound0_sound = new Sound( ); // Correct. This targets the current timeline. mySound1_sound = new Sound(this); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required