Sound.attachSound( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Sound.attachSound( ) Method Flash 5

associate a sound from the Library with a Sound object
soundObject.attachSound(linkageIdentifier)

Arguments

linkageIdentifier

The name of the sound to attach, as specified in the Library under Options figs/u2192.gif Linkage.

Description

The attachSound( ) method places an exported Library sound under soundObject's control. Once attached, the sound can be started and stopped individually by invoking start( ) and stop( ) on soundObject. For instructions on pausing a sound, see the online technote posted at:

http://www.moock.org/asdg/technotes/pauseSound/

In order for a sound to be attached to soundObject, the sound must be exported from the movie's Library. To export a sound, follow these steps:

  1. In the Library, select the sound to export.

  2. Select Options figs/u2192.gif Linkage. The Linkage Properties dialog box appears.

  3. Under Linkage, select Export For ActionScript.

  4. In Flash 6, optionally uncheck the "Export in first frame" checkbox.

  5. In the Identifier box, enter a unique name by which to identify the sound.

In Flash 5, all exported sounds are loaded in the first frame of the movie that contains them (not when they are actually attached or played via ActionScript), which can cause long load delays if the sounds are large.

In Flash 5, you can gain better control over the loading of sounds by placing them in external .swf files and using loadMovie( ) or MovieClip.loadMovie( ) to import them as necessary.

In Flash 6, attached sounds can be exported on any frame. In the Linkage Properties dialog box, uncheck "Export in first frame," and then place the sound on the movie's main timeline at the desired export frame. The attachSound( ) method will work only after that frame. Alternatively, use loadSound( ) to retrieve an external sound file when one is needed.

Only one sound can be attached to a Sound object at a time, but you can create up to eight simultaneous Sound objects. Attaching a new sound to a Sound object replaces any sound previously attached to that object. To keep multiple attached sounds in synch, attach them to separate Sound objects and start them all in a single frame, then use Sound.setVolume( ) to silence each sound or make it audible as necessary.

When a movie with an exported sound is loaded into a target level or movie clip, the exported sound can be attached only if the Sound object's specified target matches that level or clip. For example, suppose we've stored exported sounds in linkedSounds.swf, and then we load linkedSounds.swf into a movie clip named host. When the .swf file has completely loaded, we can safely attach and play its exported sounds as follows:

// Create a new sound with a target of host host.bang = new Sound(host); host.bang.attachSound("loudBang"); host.bang.start();

It's a common mistake to omit the target argument when constructing Sound objects in loaded movies. If target is not supplied, it defaults to _level0, where the exported sound cannot be found. For example, the following attempt to play a sound in host fails because the sound loudBang is not available in the Library of _level0:

// Create a new sound with an implied target of _level0. host.bang = new Sound();     // This operation fails..."loudBang" is not in _level0's Library. host.bang.attachSound("loudBang");     // The sound won't start because it wasn't successfully attached. // We forgot to specify a target when we constructed host.bang. host.bang.start();

From within a movie that will be loaded into other movies, we can ensure that exported sounds will always be attachable by using the this keyword as the target parameter of all Sound objects. For example:

// Code inside sounds.swf, which will be loaded into other movies music = new Sound(this);  // Set target to current movie clip music.attachSound("song"); music.start();

Example

The following example adds a sound with the identifier phaser to the Sound object phaserSound. It then starts and stops the phaser sound. We need not specify the target in the constructor, provided this is the main move and not a movie loaded into a movie clip:

phaserSound = new Sound(); phaserSound.attachSound("phaser");     // Start the phaser sound phaserSound.start();     // Stop just the phaser sound phaserSound.stop("phaser");

See Also

Sound.loadSound( ), Sound.start( ), Sound.stop( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net