Lesson 4

Use ActionScript with Sound

 

OBJECTIVE:
In this lesson, you will use ActionScript to play and stop sounds.

Sample Solution

Lesson:  Link a Sound File with a Movie

  1. Open fl10_3.fla, then save it as FL_C10_L3_levels (your username) to your Flash C10 folder on the server
  2. Click Control (on the menu bar), then click Test Movie
  3. Click the start button, then click the mute button—the animation plays and stops, but there is no sound
  4. Close the Flash Player window
  5. Display the Library panel, right-click the song 1 audio file, then click Properties
  6. Click the Advanced button to display the Linkage area, then click the Export for ActionScript  check box to select it—when you click the check box, Flash adds the sound name to the Identifier field and automatically selects the Export in frame 1 check box
    [TIP:  The identifier is the name you will use to reference this sound clip in ActionScript]
  7. Click OK

Summary:  You used the Sound Properties dialog box to create a linkage identifier string and to make sure a sound will be exported for ActionScript.

 

 

Lesson:  Start a Sound using ActionScript

  1. Click the Selection tool (on the Tools panel), click the start button to select it, then open the Actions panel—the current ActionScript code causes the playhead to go to the frame labeled 'startBars' when the start button is clicked—the 'startBars' frame is the start of the animation
  2. Verify that Script Assist is turned off, then verify that the start_music button symbol appears in the lower left of the Script pane
  3. Click the View Options button (in the Actions panel), then click Line Numbers if it does not already have a check mark next to it
  4. Click the end of Line 2 in the Script pane (after the semicolon), press [Enter] to create a new Line 3, then type

    music=new Sound();

    Your screen should look like the following:

    on (release) {
    this colorBars (gotoAndPlay) (“startBars”);
    music=”new Sound()”


    }

    Be sure you have a space between new and Sound—this ActionScript statement creates a new sound object named “music”
  5. Press [Enter] to create a new Line 4, then type music.attachSound(“song1”);
    This ActionScript statement attaches 'song1' to the sound object based on the linkage identifier
    [Note:  A list of actions appeared as you were typing the code—you can double-click an action to insert it into the code as a shortcut]
  6. Press [Enter] to create a new Line5, then type music.start();
    This ActionScript statement plays the sound object
    Your ActionScript should now look as follows:

    on (release) {
    this colorBars (gotoAndPlay) (“startBars”);
    music=”new Sound()”
    music.attachSound(“song1”);
    music.start();


    }
  7. Click Control on the menu bar, click Test Movie to test the movie, click the start button, then click the mute button—the sound file plays when you click start; when you click mute, the color bars stop playing, but the sound file does not
  8. Close the Flash Player window

Summary:  You wrote an ActionScript for a button that creates a sound object, attaches a sound to the object, and plays the sound.

 

Lesson:  Stop Sounds using ActionScript

  1. Collapse the Actions panel, click the mute button on the Stage to select it, then display the Actions panel
  2. Verify that the mute button symbol appears in the lower left of the Script pane
  3. Click Line 4 in the Script pane of the Actions panel
  4. Click the Add a new item to the script button, point to Global Functions, point to Timeline Control, click stopAllSounds
  5. Collapse the Actions panel, click the start button on the stage to select it, then display the Actions panel and verify that the start_music button symbol appears in the lower left of the Script pane
  6. Click the end of Line2 in the Script pane of the Actions panel, then press [Enter] to create a blank Line 3
  7. Click the Add a new item to the script button, point to Global Functions, point to Timeline Control, click stopAllSounds—inserting stopAllSounds ensures that the sound starts at the beginning each time the visitor clicks the button and that multiple copies of the sound file do not play at the same time
  8. Close the Actions panel, click Control on the menu bar, then click Test Movie
  9. Click the start button, click the mute button, then click the start button again—the music plays when you click start, then stops when you click mute
  10. Close the Flash Player window, save your work
  11. Publish the movie
  12. Post the movie to the Hulk server
  13. Then close the movie

Summary:  You added actions to stop sounds from playing.