I l @ ve RuBoard |
The first question many developers have when learning ActionScript is "Where do the scripts go?" Flash is a complex multimedia authoring environment. If you have used Flash for a while, or have just run through the tutorials that come with Flash MX, you know the basic elements that make up this environment. There are three places where scripts can be placed among these elements. Scripts in the TimelineEach scene in a Flash movie has a timeline. Each key frame in this timeline can hold a script. In addition, you can have multiple scripts on the same frame by placing them in different layers in the timeline. To place a script in the main timeline, first select any key frame. If you are starting a new movie, there should be one empty key frame in the timeline. Figure 1.1 shows an empty timeline, with the default key frame selected. Figure 1.1. This is how the timeline looks when you create a new Flash movie. A script can be placed on the one key frame that is automatically inserted for you.
With a key frame selected, you just need to bring up the Action panel to see what script is there, or to start writing a new script. There are several ways to bring up the Actions panel. You can choose Window, Actions from the menu, or use the keyboard shortcut F2.
Figure 1.2 shows the Actions panel. The Actions panel is actually titled Actions ”Frame because the script will be applied to this frame. If this is a new movie, the Actions panel will be blank. We'll soon fill it with all sorts of scripts. In Hour 2, "Using the Script Editing Window," we'll look at how to use the Actions panel. Figure 1.2. The Actions panel is where you view scripts placed in a frame.
Scripts in the timeline execute when that point in the timeline is encountered as Flash is playing back the movie. For instance, if you place a stop() command in the script attached to a key frame, the movie will stop moving forward at that point. It will continue only if another script tells it to. Another reason to place scripts in the timeline is when you want to use functions as part of your ActionScript code. Functions are bits of code that can be reused. For scripts throughout the movie to call these reusable functions, they must be placed in the main timeline. Scripts Attached to Buttons
The third type of symbol, buttons, can also have scripts attached. As a matter of fact, buttons are useless without scripts ”they can't do anything without scripts. To attach a script to a button, first select the button on the stage. Then choose Window, Actions from the menu. Or use the keyboard shortcut F2. Notice that the Actions panel has a different title. It looks just like the window in Figure 1.2, but this time, it is titled Actions ”Button. As you might guess, scripts attached to buttons usually contain instructions for Flash to perform if the user clicks the button. Scripts can also react to when the mouse enters or leaves the button's space. Buttons can also react to key presses. This makes it easy to script a button that has a keyboard shortcut. Scripts Attached to Movie ClipsMovie clips differ from plain graphic symbols in that they can be named, and they can have scripts attached to them. You can attach a script to a movie clip in the same way that you attach a script to a button. Scripts attached to movie clips can be used to control that movie clip or to control other movie clips on the same timeline. Your scripts can detect when a movie clip first appears on the screen, and when a frame in the timeline has passed. This allows you to write scripts that execute repeatedly, once each frame. You can use such scripts to control animation. In addition to attaching a script to a movie clip, you can also place scripts inside movie clips. After all, a movie clip is just another Flash movie. Inside a movie clip, there is another timeline. You can place timeline scripts on this timeline just like you can with the movie's main timeline. You can even place buttons inside a movie clip and attach scripts to it. A complex example would be a button with a script inside a movie clip, which is inside another movie clip, which is on the main timeline. |
I l @ ve RuBoard |