Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Flash

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Fading a Sound

Although fading effect can be applied by hand to static sounds, dynamic sounds require a little more finessing. The trick is to alter the volume of the sound over time, using ActionScript.

The most common mistake when scripting fades is to use a for loop on a single frame, where the volume starts at a certain level and ends at another level. Such loops are not effective because frame scripts are executed completely before Flash Player updates the contents of the stage. Thus, the sound is heard to jump abruptly from one volume to another.

A more effective method is to use events to drive the transformation. The enterFrame event is fired several times per second, according for the frame rate of the movie. By setting up a movie clip with an enterFrame handler, the volume of the sound can be altered over time, like so:

onClipEvent(enterFrame) {

	CurrentVolume = root.MySound.getVolume();

	if (CurrentVolume > 0) {

		_root.MySound.setVolume(CurrentVolume - 1);

	}

}

(This sample script assumes that the sound is on the main timeline and is named MySound, but the details can be altered for any situation.)

Each time the enterFrame event fires, the volume of the sound is reduced by 1 percent. Changing the 1 to some other number hastens the fade.

  • + Share This
  • 🔖 Save To Your Account