Creating and Attaching Clips
The createEmptyMovieClip() method is great for creating movie clips while the SWF file plays. It not only means that you can avoid creating each movie clip manually, you can even control what kind of movie clip that you create in the SWF file depending on user interaction. In the case of this project, though, it means that you can create a series of movie clips automatically that are exactly the same as each other, differing only in what text appears on the face of the button. You can also make sure that each button executes in a specific way when the user clicks it.After you create the movie clip at runtime, you will attach it to the Stage at a specific location. You also specify an instance name so you can control the movie clip any way you want by using code (such as giving it the button functionality). In Exercise 4, you attach the buttons to the Stage. First, you create the text fields that will create menu buttons in Exercise 3; then you attach and position them on the Stage.
Exercise 3: Creating dynamic menu buttons
Before you can write the code that creates the navigation system, you need to create a movie clip in the Library. This movie clip later attaches to the Stage using the navButton() function, outlined on the following pages.
1. Select Insert > New Symbol to create a new movie clip symbol. Give it a symbol name of navBtn, select the Movie Clip behavior, and click OK to open navBtn in symbol editing mode.2. Select the Text tool, and click the Stage to create a new text field. With the text field still selected, open the Property inspector. Select dynamic from the Text type pop-up menu, and give it an instance name of label_txt. Position the text field at the X coordinate of 10 and Y coordinate of 7, ensure that the Line type pop-up menu is set to single line, Show border around text button is toggled off, and that the Selectable button is toggled off (Figure 9.5).
Figure 9.5. Set these text properties in the Property inspector.
[View full size image]

Figure 9.6. Embed text using the Character Options dialog box. Specify a range of characters to embed.

Figure 9.7. Import bg.png onto the menu layer, and place it at 0,0.
[View full size image]

Exercise 4: Attaching the navigation movie clip
Now that the navBtn symbol is in the current Library and has been assigned the proper linkage identifier, you can attach the movie clip to the Stage dynamically by using the attachMovie() method, as demonstrated in the following steps.
1. Select frame 1 of the actions layer and open the Actions panel. Delete these two lines of ActionScript.
After you delete the two lines of code, type the following code at the bottom of the existing ActionScript in the Script pane:
this.createEmptyMovieClip("rectangle_mc", 100);
drawRectangle(rectangle_mc, 0xFFFFFF, 40, 320, 240);
This code first defines a new movie clip, named root_mc, which points to the current Timeline. You use this value throughout the script to let you easily access the main Timeline without having to use long paths or resort to using _root.The next line of code defines the navButton() function, which takes four arguments (instanceName_str, label_str, target_str, and subMenu_array) and doesn't return a value. These arguments let you define the instance name for the new button, the text that appears in the navigation button, the URL that the SWF file redirects to if a user clicks the button, and which subnavigation items are visible when the user rolls the mouse cursor over a button in the main menu.
var root_mc:MovieClip = this;
function navButton(instanceName_str:String,
label_str:String, target_str:String,
subMenu_array:Array):Void {
var target_mc:MovieClip =
root_mc.createEmptyMovieClip(instanceName_str,
root_mc.getNextHighestDepth());
var box_mc:MovieClip =
target_mc.createEmptyMovieClip("box_mc",
target_mc.getNextHighestDepth());
var label_mc:MovieClip =
target_mc.attachMovie("navBtn", "label_mc",
target_mc.getNextHighestDepth(), {_x:_global.navXPos,
_y:0});
}
About _root
You should avoid using _root whenever possible. The _root identifier refers to the main Timeline of the SWF file whether the code is in a movie clip or on the main Timeline. It's called absolute addressing, much like putting the entire address of a web page starting with http:// in you115 code. However, if you have other content loading into a SWF file, using _root can cause problems. If those clips or components use _root, it targets the main Timeline of the SWF file they load into. This could cause your ActionScript to break because _root in the code targets the wrong Timeline. Instead of targeting the main Timeline of the SWF it is in, it targets the main Timeline of the SWF file it's loading into.Therefore, instead of using _root, you should try to use the this keyword or _parent addressing (known as relative addressing). This addressing is used throughout the code in this book.The navButton() function creates the main navigation buttons, positions them on the Stage, and creates the subnavigation for each button. You base the subnavigation on the value of the subMenu_array parameter. For example, the subMenu_array is an array of objects and each object in the array represents a single subnavigation item. So the news button you create later has two subnavigation links (current news and archived news) that point to differen117 pages.
Figure 9.8. Creating and positioning buttons and subnavigation buttons on the Stage.

The bold face code sets the value in the label_txt text field to the value of the navButton() function argument label_str. Next, you set the autoSize method of the label_txt text field to true so that the text field automatically resizes to fit the text. Now, whether you set the value of label_str to something short (such as "home") or something long (such as "frequently asked questions"), the text field shrinks or grows to properly fit the content. The last line of code sets the _x position of the box_mc movie clip to the same position as the label_mc movie clip. By default, movie clips created using the createEmptyMovieClip() method position content at 0, 0.3. Add the following code directly after the code you added in step 2 (still inside the navButton() function):
function navButton(instanceName_str:String,
label_str:String, target_str:String,
subMenu_array:Array):Void {
var target_mc:MovieClip =
root_mc.createEmptyMovieClip(instanceName_str,
root_mc.getNextHighestDepth());
var box_mc:MovieClip =
target_mc.createEmptyMovieClip("box_mc", target_mc.getNextHighestDepth());
var label_mc:MovieClip =
target_mc.attachMovie("navBtn", "label_mc",
target_mc.getNextHighestDepth(), {_x:_global.navXPos,
_y:0});
label_mc.label_txt.text = label_str;
label_mc.label_txt.autoSize = true;
box_mc._x = label_mc._x;
}
The previous snippet sets two variables, txtWidth and txtHeight, which represent the width and height of the label_txt text field while adding some padding.
var txtWidth:Number = label_mc.label_txt._width+20;
var txtHeight:Number = label_mc.label_txt._height+10;
drawRectangle(box_mc, 0xFFFFFF, 40, txtWidth, txtHeight);
box_mc._alpha = 0;
_global.navXPos += txtWidth;
Figure 9.9. When you click a Color control in the Tools panel, a color pop-up menu opens where you can select a swatch.

Figure 9.10. When you roll over a button, the subnavigation appears under the main menu, and a rectangle appears behind the main menu button because the _alpha of the box_mc clip sets to 100%.

[View full width]box_mc.onRollOver = function() {This code creates the three event handler functions mentioned in step 4. The code for the onRollOut and onRelease events is complete, but the code for onRollOver is a bit long and will be added in a future step. When you roll your mouse off of the box_mc movie clip, the box_mc movie clip fades out using ActionScript's Tween class. You use the Tween class to fade box_mc's _alpha property from 100% to 0% across seven frames.
// code to be added in upcoming exercise...
};
box_mc.onRollOut = function() {
new mx.transitions.Tween(this, "_alpha", mx.transitions.easing.None.easeNone, 100, 0, 7,false);
};
box_mc.onRelease = function() {
getURL(target_str);
};
You added a lot of ActionScript in this exercise, so remember to save your changes before you move on to the next exercise. To see the finished ActionScript, look in frame 1 of the actions layer in the completed menu.fla on the CD-ROM inside the 09/complete folder. In the following section, you add the buttons that this code works with to create an animated menu.
Code Readability
When you write ActionScript, it is important that you find it easy to read your code. This means that you can scan through your code easily, looking for errors, places to add more ActionScript, or have other people who read your code (or yourself) understand what's occurring in the script.There are several ways to help make your code readable. One of those ways is to use descriptive variable names: name your variables in a way that they describe what content they contain. Another way is to add comments within your code to tell you and others what various sections of the code accomplish. A third way is to add white space within your ActionScript. This means that you separate sections of code by a few empty lines (press the Enter/Return keys a few times). This helps separate the ActionScript into "paragraphs"each paragraph contains a separate function or purpose within your FLA file or application.For more information on code readability, read the Best Practices guide for Flash in Flash documentation (F1). Search for "best practices" within the Help, or look for the "Using Best Practices" chapter within the Using ActionScript in Flash book.
Using the Tween Class
The Tween class (introduced in Flash MX 2004) gives developers and designers the ability to easily animate objects on the Stage without having to write lots of code or to manually create motion tweens. For example, in onRelease event handler to use either the gotoAndStop() or gotoAndPlay() functions rather than getURL().For more information on the Tween class, go to the book's website at [www.FLAnimation.com].
Exercise 5: Creating dynamic sub menu buttons
Creating a submenu button movie clip is similar to the process of the menu buttons created earlier in this chapter. You need to create a new movie clip in the Library, add a text field, and define a few properties.
1. Click the Create new symbol button in the Library to launch the Create New Symbol dialog box. Enter a symbol name of subNavBtn and set the behavior to Movie clip.2. Before you click the OK button, click Advanced in the Create New Symbol dialog box. The advanced mode of the Create New Symbol window lets you set linkage information without having to set the linkage information later using the Linkage Properties dialog box. In the Linkage section of the Create New Symbol dialog box, click the Export for ActionScript check box and enter an Identifier name of subNavBtn. Click OK.
Figure 9.11. Setting Linkage in the Create New Symbol dialog box.

Figure 9.12. Create a text field and set its properties.
[View full size image]

Embedding the text outlines for the font you use in the menu ensures that the font appears the same on every computer system. If you do not embed fonts, and the user doesn't have the font you specify, then the SWF file defaults to a similar font that is available on their computer system. However, this means the menu appears differently than the one you designed.