Turn Down That Noise


Turn Down That Noise

Whenever you plan to use sound in a presentation, please give your audience a way to turn the sound off. Nothing drives people away as fast as a booming audio track with no Off button especially if they're in the office within earshot of the boss.

What do you want to accomplish here? You press a button, the sound goes off. You press a button, the sound comes back on. How hard can that be? Put on your ActionScripting shoes for this one (not too bad, I promise). You haven't gotten into object-oriented programming and the Flash objects yet, so you'll be doing this the old-fashioned way.

Turning the sound off is the easy part. In Flash ActionScript, there is an Action that stops all sounds (stopAllSounds). Using this action stops every sound that is currently playing. You should be forewarned that if any of your sounds are in movie clips that loop, the sound restarts as soon as the movie or movie clip begins to play again. You can get around that by setting up some variables to capture information about whether a button has been pressed.

Note

Just in case you don't remember from earlier chapters, a variable is just a placeholder for information. In this case, the variable is music and the value of music is true.


Exercise 13.6 Turning Off the Sound

Start by turning off just the music.

  1. Open the NewYork.fla file if it isn't already open or open NewYork1.fla from the CD.

  2. Select the Music On Off button in the Sound Toggle layer. Launch the Actions panel. For now, set the Actions panel to Normal mode by using the Options pop-up menu.

  3. Double-click the Actions category in the Toolbox list to expand it. Scroll down until you see the stopAllSounds action. Double-click stopAllSounds to add it to the Actions list.

    Because you're in Normal mode, Flash was smart enough to recognize that you're adding an action to a button, so it added the button event, on (release), for you. (See Figure 13.9.)

    Figure 13.9. When you add an Action to a button in Normal mode, Flash automatically adds an on (release) mouse event.

    graphics/13fig09.gif

  4. Save your file and test it. Try clicking the Sound On Off button to see what happens.

You can turn the sound off, but you can't turn it back on. There is no "start all sounds" option. To restart the sound, you have to get a little creative.

Exercise 13.7 Restarting a Sound

When the movie starts to play for the first time, the sound is on. Why not set a variable in the main timeline that reflects that? You'll set a variable called music and give it a value of true. Then, when the On Off button is clicked, you can change the value to false.

  1. Select frame 1 of the Actions/Labels layer in the main timeline. Launch the Actions panel. Double-click Set Variable in the Tool list.

  2. In the Parameters pane (see Figure 13.10) fill in the following:

    Variable: music

    Value: true (make sure Expression is checked)

    Figure 13.10. In Normal mode, you can fill out the necessary Parameters in the Parameters pane.

    graphics/13fig10.gif

  3. Select the On Off button on the Stage. In the Actions panel, select the line that begins with on (release).

    When the button is clicked, you want to check whether the current value of music is true. If it is, you want to stop all sounds and you want to change the value of music to false. That means the sound is on.

  4. In the Toolbox list, double-click if. In the Parameters pane, type music==true. (See Figure 13.11.)

    Figure 13.11. Use an if statement to test whether the music variable has been set to true.

    graphics/13fig11.gif

    Note

    What does the == mean? That's the equality operator. It tests to see if the expressions on either side are equal to one another. If they are, it returns true. If not, it returns false. What's the difference between == and =? Using the operator == tests for equality; using = sets the value of a variable.

  5. With the first line of the if statement selected, double-click Set Variable again and change the settings in the Parameters pane to the following:

    Variable: music

    Value: false (make sure Expression is checked)

  6. Right now, the stopAllSounds action is outside the if statement. Move it inside the if statement by clicking stopAllSounds and dragging it inside the if. Your ActionScript should look like the following:

     on (release) {    if (music==true) {         music = false;          stopAllSounds ();     }  } 

    So far, so good. But what are you going to do if the value of the variable music is equal to false? You need to test for that and figure out how to restart your sounds.

  7. Highlight the stopAllSounds line. Double-click else if in the Toolbox list. In the Parameters pane for Condition, type music=false.

    If the variable music is false, that means the sound is currently turned off.

    You next need to restart two separate movie clips: Soundtrack and Voiceover. Before you can restart the two movie clips, you need to assign their instances on the Stage unique names so that you can use ActionScript to talk to them. These two movie clips are the open circles on the Stage.

  8. Select the Voiceover layer. The Voiceover movie clip on the Stage is now selected. Open the Instance panel and give the movie clip the name Voiceover .

  9. Select the Soundtrack layer to highlight the Soundtrack movie clip on the Stage. Open the Instance panel and give the movie clip the name Soundtrack.

  10. Open the Actions panel and select the On Off button again.

  11. Select the line that reads else if (music == false). If you reach this part of the script during processing, the sound is already off; you want to turn the sound back on and set the value of the music variable back to true. Double-click set variable in the Toolbox list and change the settings in the Parameters pane to the following:

    Variable: music

    Value: true (make sure Expression is checked)

    Your code at this point should look like the following:

     on (release) {     if (music == true) {         music = false;          stopAllSounds ();      } else if (music == false) {         music = true;      }  } 
  12. Use the Options pop-up menu to switch to Expert mode. (See Figure 13.12.) For the next three lines of code, you'll find it easier to set up the proper dot syntax if you're in Expert mode. It's a little tricky to get the syntax right for the movie clips you're targeting in Normal mode.

    Figure 13.12. Use the Options pop-up menu to switch to Expert mode.

    graphics/13fig12.gif

    Note

    If you're ever unsure of the path to a particular movie clip, you can use the Insert a Target Path button in the lower-right corner of the Actions panel. It's the icon that looks like a target with crosshairs. Whenever it's shaded dark blue, you can click it to launch the Insert Target Path dialog box. You'll see a hierarchical tree of the named movie clips in your movie. Notation can be either dot or slash syntax. Use dot syntax; slash syntax is deprecated. You also can choose whether the path should be relative to the movie clip you're currently in or whether it should be absolute, that is, based from the root. I usually use absolute paths when I'm working on complex movies, but relative pathing is fine for simple movies.

  13. Position your cursor at the end of the line you just added (music = true;) and add a new line by pressing the Return or Enter key. Tab twice and type:

     Voiceover.gotoAndPlay("Restart"); 

    What does that do? You are telling Flash to go the movie clip named Voiceover and play the frame labeled Restart. If you open the Voiceover movie clip, you'll see that there is a frame labeled Restart right where the voice-over and animation start.

  14. You are going to do something similar for the Soundtrack movie clip, but instead of going to a labeled frame, you'll just go to frame number 1. Add a new blank line, tab twice, and type:

     Soundtrack.gotoAndPlay(1); 
  15. It would be nice to restart the background animation when you restart the sound. That movie clip already has an instance name of NewYork. It also has a frame labeled Restart. Add a new blank line, tab twice, and type:

     Soundtrack.gotoAndPlay(1); 

    Now your code looks like the following:

     on (release) {     if (music == true) {         music = false;          stopAllSounds ();      } else if (music == false) {         music = true;      voiceover.gotoAndPlay("Restart");          soundtrack.gotoAndPlay(1);          NewYork.gotoAndPlay("Restart");      }  } 

    Before you test your movie, you'll need to add a couple more lines of code. What happens if you've made a mistake and the variable music isn't set to either true or false? You'd like to know that. You can set a Trace action to pop up an error message for you in the Output window.

  16. You can switch back to Normal mode now. Select the line that starts with NewYork.gotoAndPlay. In the Toolbox list, double-click else. Flash adds a line that looks like this: } else { . (The line does not include the period.) With that line still selected, double-click trace in the Toolbox list, and in the Parameters pane, type the following: This is not working. (Do not type the period.)

    Do not select Expression. Your final code should look like this:

     on (release) {     if (music == true) {         music = false;          stopAllSounds ();      } else if (music == false) {         music = true;          voiceover.gotoAndPlay("Restart");          soundtrack.gotoAndPlay(1);          NewYork.gotoAndPlay("Restart");      } else {         trace ("This is not working");      }  } 
  17. Save and test your file.

If all is well, you should be able to turn the sound for the file off and on. If there are any problems, the message "This is not working" should appear in the Output window.

Any time you are using both sound and animation in a movie, it's a good idea to preload all the elements in your movie to make sure everything runs smoothly. You'll take a look at how to add a simple preloader next. Preloaders are discussed in more detail in Chapter 16, "Using Preloading Sequences."



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

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