How to Play Sounds With a Flash Rollover

Flash can be used to add sound to a file in either of the two ways. One way is by use of the time-line frames where sound and rollover objects are attached and play when the animation reaches that frame. Alternatively, and a better method, is to use ActionScript. Writing ActionScript code allows you to attach code snippets onto the time frame. This method allows large code snippets to be added without an increase in the flash size.

Instructions

  1. Adding Sound

    • 1

      Import an object, either an image, file text or other object that you want to have the mouse-over effect.

    • 2

      Convert all of the objects to be used for the rollover effects to either button or movie clip symbol instances by pressing F8 and selecting either a movie clip or a button instance type.

    • 3

      Ensure that your object has an instance name, such as "myButton."

    • 4

      Import the sound files into the library that you want to play by going to "File" then "Import" and then "Import to Library..."

    • 5

      Make a simple link to the object by going to the link section under "Properties" and using the "#" symbol to initialize a link.

    • 6

      Go to "Windows" then "Behaviors" and click on the "+" symbol in the panel.

    • 7

      Click "Play Sound."

    • 8

      Browse your selection of sound files and click "OK."

    ActionScript

    • 9

      Create a new layer and call it "Actions." This is where all your ActionScript code will go.

    • 10

      Assign an id (identification) to the sound to be played upon rollover by going to the "Library" and right-clicking on the sound file to be used.

    • 11

      Select the pop-up menu and check the "Export to actionscript" option.

    • 12

      Type "rollover" in the text box to assign an id to the sound.

    • 13

      Select the first frame of the "Actions" layer on the time line and click "F9."

    • 14

      Add the following code to the code window:

      Var snd = new Sound();
      Snd.attachSound("rollover");

      This code creates a sound object and attaches the sound file to it.

    • 15

      Copy to frame 1 on the "Actions" layer of the time line the following code:

      newSound = new Sound();
      newSound.attachSound("rollover");

      myButton.onRollOver = function(){
      newSound.start(0,1);
      };

      myButton.onRollOut = function(){
      stopAllSounds();
      };
      myButton.onRollOut = function(){
      stopAllSounds();
      };
      myButton.onDragOut = function(){
      stopAllSounds();
      };

    • 16

      Stop the "rollover" sound specifically by using the code below:

      newSound = new Sound();
      newSound.attachSound("rollover");

      myButton.onRollOver = function(){
      newSound.start(0,1);
      };

      myButton.onRollOut = function(){
      stopAllSounds();
      };
      myButton.onRollOut = function(){
      newSound.stop();
      };
      myButton.onDragOut = function(){
      stopAllSounds();
      };

      Notice the function code change on the "myButton" from:
      myButton.onRollOut = function(){
      stopAllSounds();
      };
      to

      myButton.onRollOut = function(){
      newSound.stop();

    • 17

      Press "Ctrl" and "Enter" and run your application.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured