Integrating Sound

One thing that has been conspicuously absent from this book has been the use of sound. While sound is not directly a part of animation, well-done sound effects can go a long way to making a Flash movie more immersive and realistic.

You can use sound in Flash in different ways. You could add a musical or environmental background track that just plays while your movie is running. In this case, you simply set up the sound to start playing when the movie starts. The easiest way to do that would be to import the music file into the library (by selecting File image from book Import image from book Import to Library). Then, open the library and drag the new sound item onto the stage in frame 1. Now the sound will play as soon as your movie starts.

If it is a long song, it might be OK that it just plays once, but if it is a shorter sound, you probably want it to loop. To do this, select the frame that the sound is on, and then look at the Properties panel. Youll see a drop-down list displaying the option Repeat, and next to that a text field with the number 1 in it. You can enter another number to have the sound file play multiple times, or just change the Repeat to Loop, and it will play indefinitely.

But far more relevant to animation would be sounds that play when a certain event happens in a movie. The most obvious would be a collision. A ball hits a wall or another ball, and you hear bang or boing or splat or whatever. For this, a simple timeline-based sound wont do. You need the ability to start the sound via ActionScript.

For this example, Im again going back to ch06_04.fla , with the ball bouncing off the walls. Each time it hits a wall, I want it to make a sound. The new file is saved as ch19_14.fla .

First, you are going to need a sound effect to use. There are many sources of free sound effects on the web. One of the most popular sites for sound for Flash is good old FlashKit. In addition to their loops section, which are musical files, they have a sound effect library at http://www. flashkit .com/soundfx/. The effects are organized by categories such as Cartoon, Interfaces, Mechanical, etc., and the site has well over 6,000 sound effect files to date, so you should be able to find something there to suit your needs. You can preview the effects right on the page, and when you find one you like, download it. Either wav or mp3 format will be fine. Save it to your hard disk and then import it to your library as demonstrated previously.

After I import a file sound, I usually double-click it and change its name to something more concise . Flash uses the full file name, along with the extension, as a library item name, which is usually a bit much to type in code. For this example, I downloaded a boing sound, so I just renamed it boing .

Now, to make a sound available to your code, you have to export it for ActionScript. This is done in exactly the same way as exporting a movie clip symbol for ActionScript, as covered in Chapter 2. I chose the default linkage name, boing . Now the sound is ready for use.

To use sounds in ActionScript, you need to create a Sound object. This is as simple as saying something like

 mySound = new Sound(); 

Sound objects are always tied to a particular movie clip instance. If you create it as shown previously, it will be tied to the main timeline, or _root . That is fine for this simple example, but if you have multiple sounds and want to control things like their volumes and panning independently (we wont be getting into that here), youll want to assign them to different movie clips, like so:

 sound1 = new Sound(mc1); sound2 = new Sound(mc2); 

Now that you have a Sound object, you need to tell it what sound file to play. There are different options here, such as loading an external mp3 file, or even streaming an mp3 from a server. But for the purposes of this example, you want it to play the sound you just imported into the library. You do that with attachSound , giving it the linkage name of the sound you want to play.

 mySound.attachSound("boing"); 

Now your sound is ready to play at your command. All you have to do is say

 mySound.start(); 

at any point and the sound effect will play. There are optional parameters to start , such as the seconds to offset the sound, and how many times to play the sound, but the default use as shown will play the sound once from the beginning, which usually serves the purpose. Here is the full code for ch19_14.fla , showing the creation of the Sound object, and you can see that whenever the ball hits a wall, it plays that sound.

 init(); function init() {       top = 0;       left = 0;       bottom = Stage.height;       right = Stage.width;       ball = attachMovie("ball", "ball", 0);       ball._x = Stage.width / 2;       ball._y = Stage.height / 2;       vx = Math.random() * 10 - 5;       vy = Math.random() * 10 - 5;  boing = new Sound();   boing.attachSound("boing");  } function onEnterFrame(Void):Void {       ball._x += vx;       ball._y += vy;       if (ball._x + ball._width / 2 > right)       {  boing.start();  ball._x = right - ball._width / 2;             vx *= -1;       }       else if (ball._x - ball._width / 2 < left)       {  boing.start();  ball._x = left + ball._width / 2;             vx *= -1;       }       if (ball._y + ball._height / 2 > bottom)       {  boing.start();  ball._y = bottom - ball._height / 2;             vy *= -1;       }       else if (ball._y - ball._height / 2 < top)       {  boing.start();  ball._y = top + ball._height / 2;             vy *= -1;       } } 

Test the movie and see . . . hear the difference that sound can make. Of course, finding the right sounds to use in the right circumstances, and not overdoing it, is an art in itself.



Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

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