- ActionScript Elements Used to Make Games and Toys
- Lesson 15: Controlling the Playback of a Movie Clip
- Lesson 16: Controlling the Properties of a Movie Clip
- Lesson 17: Dragging and Dropping Movie Clips
- Lesson 18: Movie Clips and Levels
- Lesson 19: Duplicating Movie Clips
- Lesson 20: Controlling Multiple Movie Clips
- Lesson 21: Detecting Collisions
- Lesson 22: Using Movie Clips to Change What Is on the Screen
- Lesson 23: Accepting Keyboard Input
- Lesson 24: Playing Sounds
Lesson 24: Playing Sounds
To conclude the ActionScript tutorial part of this book, you'll see how to use sounds. Flash has the capability to play sounds by placing them in the timeline of the movie, or placing them in a movie clip in the timeline. However, you can also trigger sounds by using ActionScript commands.
Unfortunately, triggering a sound from the library is not as simple as one command. First, you have to make sure that the sound is included in the final .swf file. Do this by setting its Symbol Linkage Properties. The "Lesson 20: Creating an 'Actions' Movie Clip" section showed you how to do this. For this example, let's assume that the sound is linked with the name "beep."
You still have to use several lines of code to get the sound to play. First, you need to create a variable that is of type Sound. Next, you have to tell this variable that "beep" is the sound to play. Then, you have to trigger that sound. Here's how the code looks inside a button script:
on (press) { mySound = new Sound(); mySound.attachSound("beep"); mySound.start(); }
NOTE
The start command for sounds can also use two parameters. The first is the number of seconds into the sound before it should start. So if you want to skip the first 3 seconds, use a 3. For normal playback, use 0. The second argument is the number of times the sound is to loop. So to get 10 "beeps" in a row, use 10.
You can also do many tricks with sound. The most useful is setting the volume of the sound. This way, if you have a sound that is too loud, you can adjust the volume in your ActionScript rather than having to remake the sound.
To adjust the volume, use the setVolume command. This takes a value from 0 to 100. Here's the same code, but with the volume cut in half:
on (press) { mySound = new Sound(); mySound.attachSound("beep"); mySound.setVolume(50); mySound.start(); }
Another trick worth noting is the setPan command. This takes a value of 100 to 100. For stereo sounds, it works like a balance control with 100 being all left speaker and 100 being all right speaker. For monaural sounds, it forces the sound more from one speaker than the other. Here's a script that plays the sound from only the left speaker:
on (press) { mySound = new Sound(); mySound.attachSound("beep"); mySound.setPan(-100); mySound.setVolume(100); mySound.start(); }
One very frustrating thing about sounds is that when you set the volume or pan, it remains set, even when you want to play a completely different sound later. So if you use setPan or setVolume just once, get into the habit of setting them every single time. Otherwise, the previous setting is used for the new sound.
Example file: Sound.fla
The example movie Sound.fla has four buttons. The large middle one plays the sound normally. The large buttons to the left and right play the sound only from the left or right speaker. The smaller button in the middle plays the sound at 50% volume.
You can learn even more about sounds in Chapter 6, "Toys and Gadgets," in the section called "Jukebox." You can also learn more in Chapter 7, "Construction Toys," in the section called "Music Mixer."
This concludes the two-chapter tutorial that takes you through your first steps in ActionScript. In the next chapter, you'll learn about programming techniques such as planning, debugging, and testing. Then, beginning with Chapter 5, "ActionScript Design Effects," you'll begin to work through larger examples of programs.