Adding Movie Clip Buttons
There are several different ways to create a button in Flash. You could use a button symbol, a button component, a movie clip with an onPress or onRelease event handler defined; or you can even create movie clip based buttons with different button states (such as up, over and down). A movie clip button is a movie clip that you apply button functionality and characteristics to, using ActionScript and frame labels. Flash has a built-in functionality to understand when you want a movie clip to act as a button. All you need to do is apply an onRelease handler to a movie clip, and it's treated like a clickable button.When you add the frame labels to a movie clip, Flash knows to move the playhead to each label, depending on where the cursor is. For example, if the cursor hovers over the movie button, Flash knows o move the playhead to the _over frame (if you have one on the Timeline). However, Flash does this only after you add ActionScript for that movie clip (such as an onRelease handler).Creating movie clips with button states is a bit more involved than using a button symbol, but allows you to create intricate buttons with animations, sounds, or whatever you might add to a movie clip. You create movie clip buttons in the following exercises.
Adding Sounds to Buttons
There are different ways you can add sound to a SWF file. You can make the sound an event sound or a streaming sound. If you add an event sound, the entire sound must load before it plays, and the sound plays independent from the Timeline. A streaming sound only downloads a portion of the sound before it starts to play (great for long sound files), and it plays in sync with the Timeline. The Timeline will drop frames to keep up with the streaming sound, so they're not appropriate if you have ActionScript on the Timeline. Also, even if you play a streamed sound file more than one time, it will redownload the file from the server every subsequent time it plays. Event sounds are appropriate when you want to manipulate the sound using ActionScript, and are best for short sounds, loops, or button sounds.To add a sound to the movie clip buttons (or any button), drag a sound from the Library and drop it on a new layer inside the button movie clip or onto a frame on a Button symbol's Timeline. Use the Property inspector to set the sound to Event. The entire sound plays during its associated button event. If you place a sound in the Over frame, the sound plays when the user moves the mouse cursor over the button.
Exercise 4: Creating movie clip buttons
You begin by adding an "intro" animation for the movie clip button that spans a small number of frames. You then create a couple of different animations for the over and down states of the button. Because the built-in functionality knows to go to specific frame labels for these states, you need to only add the labels to the movie clip Timeline instead of adding a bunch of code to tell the playhead where to go when specific events occur.
1. Select Insert > New Symbol to create a new symbol, and select the movie clip behavior. Type in a symbol name of button_ animation and click OK. The new movie clip opens in symbol-editing mode.2. Inside the new movie clip, rename Layer 1 to button intro and add four more layers (from top to bottom): actions,labels,text and flash.3. Select the Rectangle tool and set the stroke color to No Stroke using the Tools panel. Set the fill color to #F3F8EB and change the alpha level to 20% using the Color Mixer panel (Window > Design Panels > Color Mixer).

Figure 8.16. Resize the shape on the Stage.
[View full size image]

Figure 8.17. Change the shape so you can shape tween the button's background.

Figure 8.18. Make settings in the Property inspector for the text field that will label each button with dynamic text.
[View full size image]



This code causes the movie to stop the playhead of the movie clip, instead of continuing to play the remainder of the frames in the instance.14. Insert keyframes on frame 12 of the actions, labels, and flash layers. Select frame 12 of the labels layer and add a frame label of _over in the Property inspector. Then add the following ActionScript to frame 12 of the actions layer:
stop();
You need to tell Flash to proceed to the next frame when using movie clip buttons because, by default, each of the button states don't move the playhead automatically. This is due to the built-in functionality of movie clip buttons mentioned previously.15. Unlock the button intro layer and click frame 6. Select the shape on frame 6 and then click frame 12 of the flash layer. Select Edit > Copy to copy the shape. Then select Edit > Paste in Place to paste the rectangle in the same position as the shape that's in frame 6. Select the shape on frame 12 and press F8 to convert it to a movie clip symbol. Enter a symbol name of button_bg and click OK.16. Insert a keyframe on frame 17 of the actions layer and add the following ActionScript code:
gotoAndPlay("nextFrame");
Insert a keyframe on frame 17 of the flash layer and change the movie clip's alpha level to 30% using the Color pop-up menu in the Property inspector. Click a frame between frame 12 and frame 16 on the flash layer, and select Motion from the Tween pop-up menu in the Property inspector.17. Insert a keyframe on frame 19 of the actions layer and add the following ActionScript:
stop();
Create a keyframe on frame 19 of the labels layer and add a frame label of _down. Finally, select frame 19 of the flash layer and press F6 to create a keyframe.18. Create a keyframe on frame 24 of the actions and flash layers. Select frame 24 of the actions layer and add the following ActionScript:
gotoAndPlay("nextFrame");
19. Select the movie clip instance on frame 24 of the flash layer and set the color drop-down to Tint. Set the tint color to #336633 and set the tint amount to 25%. Select a frame between frame 19 and 24 on the flash layer and create a motion tween using the Property inspector. See Figure 8.19 for the Timeline and how the tint changes the button's appearance.
stop();
Figure 8.19. The Timeline of a movie clip button.

Test the animation by scrubbing the playhead with your mouse and tweak the animation as needed. Save your changes to the FLA after you're finished testing the animation.
Creating Additional Button States
The advantage of using movie clip buttons is that you can add additional states to these buttons. Perhaps you want the button to change color (permanently) after the visitor has viewed a web pagemaybe called the visited state at the _visited frame label. You can do this by adding another frame label to the movie clip button, and writing your own ActionScript to determines what happens when the visitor rolls off the button (onRollOut). When the onRollOut event occurs, you can write code to move the playhead to the _visited frame and play the animation that changes the color of the button.Even though you assign a frame label of _visited in the movie clip, it's important to understand that this label has no special meaning or automatic behavior. You set the frame label to any name you want, and write code to move the playhead there when an event occurs. The underscore can be added to remain consistent with the other (built-in) frame labels.
Exercise 5: Adding the buttons to the Stage
Now that you created the movie clip buttons, you need to add them to the interface. Instead of all the buttons appearing at once, you'll add them along the Timeline shortly after each other, so they appear to tween in succession. This adds a bit of visual interest to your website.
1. If you are still inside a movie clip, click Scene 1 in the Edit Bar to return to the main Timeline. Select frame 27 of the home layer within the buttons layer folder. Press F6 to create a new keyframe, and then drag a copy of the button_animation symbol from the Library onto the Stage.2. Give the movie clip an instance name of homeBtn_mc and position the movie clip in the upper-left corner of the green rectangle that forms the background tween on the bg tween layer (Figure 8.20).
Figure 8.20. Position the first button on the Stage.

This code targets the dynamic text field nested within the button_animation symbol and sets the text field's text property to the string home. Because you use a dynamic text field for the buttons, you can reuse the same symbol for each of the four button instances that you place on the Stage.4. Create a keyframe on frame 29 of the video 1 layer, which should also be in the buttons layer folder, and the actions layer. Drag an instance of the button_animation symbol from the Library onto frame 29 of the video 1 layer, position it beneath the movie clip on the home layer, and give it an instance name of video1Btn_mc. Add the following ActionScript to frame 29 of the actions layer:
homeBtn_mc.label_txt.text = "home";
5. Repeat step 4 for the video 2 and about layers in the buttons layer folder. The keyframes for the video 2 section should be on frame 31, and the keyframes for the about section should be on frame 33.Give the video 2 movie clip button an instance name of video2Btn_mc and the about movie clip button an instance name of aboutBtn_mc, and then add the ActionScript for these two buttons:
video1Btn_mc.label_txt.text = "video 1";
Position each instance below the previous instance on the Stage, similar to Figure 8.21.
video2Btn_mc.label_txt.text = "video 2";
aboutBtn_mc.label_txt.text = "about";
Figure 8.21. Position the rest of the buttons on the Stage.
[View full size image]

Save your changes to the FLA file before moving on to the next exercise.
Exercise 6: Adding content and a transition
Buttons are nice, but they're pretty pointless if you don't have anything in your site to go to. In this exercise, you add some content to your site so it has a purpose in life.
1. Press F6 to insert a keyframe on frame 35 for each of the layers in the content layer folder, as well as the actions and label layers (Figure 8.22). Select frame 35 of the label layer and add a frame label for this frame using the Property inspector. Type content into the Property inspector.
Figure 8.22. Add keyframes to each of the layers in the content layer folder and a frame label.
[View full size image]

You'll add more code in a later step.3. Create a dynamic text field on frame 35 of the page label layer. Position the text field at the right edge of the light green box (Figure 8.23), and make sure that the text field is approximately 300 pixels wide. Use the Property inspector to check the width, but use the Selection tool to resize the text field.
stop();
Figure 8.23. Position a text field and set its properties.
[View full size image]

8. On frame 1 of the animation layer, draw a rectangle that is 10 pixels by 10 pixels. Use the Tools panel to set the rectangle's fill color to white, stroke color to No Color and set the alpha of the fill to 30% using the Color Mixer panel (make sure you click the paint bucket icon before changing the Alpha slider). Use the Property inspector to position the shape at an X and Y coordinate at 0 pixels.Then select frame 4 of the animation layer and press F6 to insert a keyframe. Use the Property inspector to resize the shape to 395 pixels wide.9. Create keyframes on the animation layer on frames 6, 10, and 13. Resize the shape on frame 6 to 190 pixels (height). Resize the shape on frame 10 and 13 to 250 pixels high. Deselect the shape on frame 6, move your cursor along the bottom-right corner of the shape, and wait until the mouse cursor changes into a little L shape. Press the mouse button and drag the right corner up slightly; then release the mouse button (Figure 8.24).
stop();
Figure 8.24. Add a transition animation to the site.
[View full size image]

The first line of code defines a new global variable called previous_mc. You use this variable throughout the remaining ActionScript to keep track of what section previously displayed on the Stage. The second line of code creates a new object that you use as a listener for a movie clip loader instance (created in the next step). The mclListener_obj object defines a single listener, onLoadInit, which you call when the target movie clip finishes loading and you're ready to display it on the Stage.The first block of code within the onLoadInit listener calls the switch() statement and triggers a block of code based on the previously displayed movie clip (see the following sidebar). If the previous movie clip was one of the video pages, the code closes the video stream that stops the video from playing back or playing audio in the background. Next, both the mask_mc and transition_mc movie clips begin to play back from their first frame, which displays a quick transition between the different sections of the SWF file.The next line of code (_global.previous_mc._visible = false;) sets the _visible property of the previously displayed movie clip to false so it is no longer displayed on the Stage, and the new section is displayed using the following code (target_mc._visible = true;). Finally the value of _global.previous_mc updates with the currently displayed movie clip.
_global.previous_mc;
var mclListener_obj:Object = new Object();
mclListener_obj.onLoadInit =
function(target_mc:MovieClip) {
switch (_global.previous_mc._name) {
case 'video1_mc' :
case 'video2_mc' :
_global.previous_mc.stream_ns.close();
break;
}
mask_mc.gotoAndPlay(1);
transition_mc.gotoAndPlay(1);
_global.previous_mc._visible = false;
target_mc._visible = true;
_global.previous_mc = target_mc;
};
Using the switch Statement
A switch statement tests a condition, and executes a block of code if the condition returns true. You test the first case statement to see if it's true, and then "fall through" to the next case if it doesn't. You should always include a default case as the last case in a switch statement. The default case executes if none of the case statements evaluate true. If nothing evaluates to true and there is no default statement, nothing happens. The break statement prevents the statement from going to the next case.16. Select frame 35 of the actions layer, and add the following ActionScript to the bottom of the Script pane in the Actions panel:
The previous snippet of code creates a new MovieClipLoader instance named my_mcl and assigns the listener object that you defined in the previous step. The final line of code in the snippet loads the home.swf file, which you find within the same directory as the site.fla file. This code loads home.swf into the home_mc movie clip instance.17. Add the following ActionScript below the code added in step 16:
var my_mcl:MovieClipLoader = new MovieClipLoader();
my_mcl.addListener(mclListener_obj);
my_mcl.loadClip("home.swf", home_mc);
The previous block of code defines four event handlers (one for each movie clip button on the Stage). Each function performs the same task: when the visitor clicks and releases a specific button, use the movie clip loader instance to load an external SWF file into its target movie clip instance on the Stage (such as "video2.swf"). Next, each event handler updates the page_txt text field on the Stage with the current section name so the user knows exactly where they are within the site.18. Save the Flash document. At this point, you can test the SWF file (Figure 8.29); however, you haven't created the external SWF files yet, so you will see some errors display in the Output panel when you test the file. Despite this, test the Flash document and see whether the transitions all work and look nice. It might be necessary to return to the FLA file and tweak animations slightly if they don't tween smoothly. Roll over each of the movie clip buttons, and make sure that they fade in and out and turn slightly darker when you hold the mouse button down over them.
homeBtn_mc.onRelease = function() {
my_mcl.loadClip("home.swf", home_mc);
page_txt.text = "V I D E O S";
};
video1Btn_mc.onRelease = function() {
my_mcl.loadClip("video1.swf", video1_mc);
page_txt.text = "V I D E O S - p a g e o n e";
};
video2Btn_mc.onRelease = function() {
my_mcl.loadClip("video2.swf", video2_mc);
page_txt.text = "V I D E O S - p a g e t w o";
};
aboutBtn_mc.onRelease = function() {
my_mcl.loadClip("about.swf", about_mc);
page_txt.text = "A B O U T U S";
};
Figure 8.29. Drag three frames to frame 10 to insert more frames.
