Setting Up a Sound Movie Clip

[ LiB ]

There are many ways to do perform the same task in Flash. You always have the option of choosing the most effective and efficient setup for yourself. In this section, you will learn how to prepare a project that can play a sound with minimal ActionScript. The advantage? There's very little coding to this method. When you're writing a complicated game and you need to squeeze in some sound effects at the last minute, this method never fails.

So I set up a file that allows you to click buttons and trigger built-in sounds with minimal ActionScript. Once you master the way I've set it up in the file, I will introduce the Sound object. But for now, open up GDA_PROG10.1.fla and examine it. See Figure 10.5 for the visual.

Figure 10.5. Demo 10.5 in action


Test the movie and click on the buttonsyou should hear sounds play. If you click the buttons fast enough, you should hear the sounds play simultaneously . This is because all of the sounds are set up as Event sounds in the Sync properties drop-down menu.

The heart of this program lies within the Movie Clip labeled Sound Clip. Double-click on it and look into it. Figure 10.6 shows its timeline.

Figure 10.6. Sound clip's timeline


As you can see in Figure 10.6, frames are labeled (noted by the red flags on the timeline) and there are scripts embedded (noted by the "a" over the keyframes). You can also see that there are three sounds embedded in three different keyframes. Let's see what each label is and what scripts are embedded.

Select the first frame and observe the Properties panelthe frame is labeled FrameOne. Now that you know that, look into the Actions panelthere's only one command in there, a stop command. The stop command causes the Movie Clip to pause at FrameOne when loaded. As there is a graphic on that frame as well, the Movie Clip will display that graphic (which happens to be the text of Sound Clip).

Frame 5 is labeled soundone and has a sound embedded on that frameno actions.

Frame 9 has the following script:


This allows the sound to play starting at Frame 5 and returning to Frame 1 when it hits Frame 9. As there is a stop command on Frame 1, everything returns to normal. A lot of jumping around, huh?



The gap between Frame 5 and 9 isn't necessary; I just wanted a neat file that can display the labela crammed file is never a good thing. Always allow yourself enough space to work with.

Frame 10 is labeled soundtwo with another sound embedded withinthe frames will play until Frame 14 where Flash encounters some more ActionScript. Here's what Flash executes:


This causes Flash to retreat once the program plays the sound.

By now, you can pretty much guess how the third sound was placed and organized into the project. Frame 15 is labeled soundthree and also has a sound embedded within. Frame 20 also has the following code:


For the very same reasons as before,this last gotoAndPlay command causes the clip to rest at the first frame and wait for another play command.

So now that you have a great idea of how the Movie Clip is set up to play our sounds, let's see the code that it takes to actually play these sounds individually.

Go back up to the main Timeline. Check out the first button on Actions panel. This is what you will see:

 on (release) {   sounds.goToAndPlay("soundone"); } 

As you can see, when the user releases the button, the gotoAndPlay command is called from the Movie Clip instance named sounds . The Movie Clip instance named sounds is the same Movie Clip that we just went over, and if you remember correctly, one of the frames within it was named soundone . This will cause the clip to play the sound clip embedded within soundone and return to Frame 1 within that Movie Clip when it's done.

The second button down on the stage has similar codeexcept this time it's playing from where the second sound clip is embedded.

 on (release) {   sounds.goToAndPlay("soundtwo"); } 

The third button plays the third clip with help of the following code:

 on (release) {   sounds.goToAndPlay("soundthree"); } 

Feel like it's starting to sink in? Let's get even deeper into this sound stuff.

[ LiB ]

Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta © 2008-2017.
If you may any questions please contact us: