Recipe 17.5. Playing a Sound with a Button


Problem

You want to add a sound to a button so that when the user clicks the button, the sound is played.

Solution

Add the sound to the Down keyframe of the button's timeline.

Discussion

You can add a sound to a button such that it is played when the user clicks the button. In order to accomplish this, you need only to add the sound to the Down frame of the button's timeline. Here are the steps to follow in order to accomplish this:

  1. Edit the button symbol.

  2. Add a new layer to the button's timeline, and name the new layer sound.

  3. If you have not yet defined over and down states for your button, do so now. Or just add regular frames to the default layer up through and including the Down frame. This step ensures that Flash will still know how to display the button when the user mouses over it and/or clicks on it.

  4. Add a new keyframe to the sound layer at the Down frame.

  5. Add the sound to the new keyframe. There are two ways you can do this: by dragging the sound from the library onto the stage or by using the Sound menu in the Property inspector.

  6. If you read Recipe 17.4, you know about how the Stream synchronization setting works. However, in the case of a button, you are adding the sound to a single frame, and the Stream setting will not work because it will not allot enough frames for the sound to play. You should choose the Event or the Start options from the Sync menu in the Property inspector. These two types of synchronization are almost the same. In each case, the sound begins playing independently of the timeline and continues to play until the sound ends or until you specifically issue a command to stop the sound. The only difference is that when you choose Event, if the user clicks the button repeatedly, the sound will play each time even if the previous sound has not yet completed. With the Start setting, the sound will begin playing only if the previous sound has finished.

After you have added a sound to a button, you may want to also take a look at Recipe 17.10, for a discussion of how to use buttons not only to start the playback of a sound, but also to stop the playback of that sound.

See Also

Recipe 17.10




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

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