|< Day Day Up >|
You've got enough of the basics of scripting down to move on to behaviors. I realize that behaviors are intended to make programming easier for novices, but in my opinion, you'll get a lot more out of them if you understand what they're doing. Behaviors simply insert several lines of ActionScript in one swoop. For scripts that require you to specify parameters, the behavior will prompt you for data. Another interesting feature of behaviors is that even after you've inserted a complete script, you can come back and make edits to it without touching the code. That is, you can use the Behaviors panel as your interface to edit the underlying code.
All this can make a programmer very nervous because she can feel out of control. In any event, I do believe it's important to see what's happening. For that reason, during the following discussion and tasks, be sure to keep open your Actions panel and watch the changes that occur in the Actions panel when you make changes to the Behaviors panel. Let's first take a tour of the Behaviors panel. Open the Behaviors panel (by pressing Shift+F3 or selecting Window, Development Panels, Behaviors) and take a look at Figure 15.11.
Figure 15.11. The Behaviors panel has several components.
The Behaviors panel has the following features:
The whole idea of the Behaviors panel is that it will insert the ActionScript code for you. Most behaviors prompt you for additional details so that parameters can be set. Also, if you need to re-edit a behavior, you can do it through this panel. Actually, you can tweak any behavior by editing the resulting code by using the Actions panel. If you edit the code through the Actions panel, however, you not only potentially break it, but the Behaviors panel can't access code after you've changed it.
(Please don't let this prevent you from trying to learn from mistakes here it's not like you'll cause some sort of meltdown.)
Using the getURL Action
Whereas the gotoAndPlay action jumps the playback head to another frame, getURL jumps the user to another web page. If you're familiar with how a hyperlink works in HTML, you should know that getURL is the same thing. With gotoAndPlay, you need to specify as a parameter the frame to which you are navigating. With getURL, you need to specify to what URL you want to navigate.
URL stands for uniform resource locator and is the address for any web page. If you want to use the getURL action to jump to my home page, for example, you need to know the URL (which is http://www.phillipkerman.com).
The following task teaches you how both getURL and the Behaviors panel work. You'll actually learn more about getURL in Hour 19, "Linking a Movie to the Web." In the following task, you'll quickly use this action to see how easy it really is.
Try It Yourself: Make a Button That Hyperlinks to Another Web Page
You'll build a hyperlink in this task. Here are the steps:
You can see that the ActionScript produced by the preceding task is the same as if you had created it using the steps in one of the earlier tasks. That is, you can also select getURL in the Actions panel toolbox under Global Functions, Browser/Network. The getURL action is nearly the same as gotoAndPlay(), except that the parameter needs to be an URL. If you want to change the event that triggers this behavior, click On Release in the Behaviors panel, and you can select from the other events available to buttons, as Figure 15.13 shows.
Figure 15.13. The Behaviors panel lets you change the event trigger without affecting the underlying code.
You can also change the destination URL by double-clicking in the Action column in the Behaviors panel. That will redisplay the dialog box that appeared in the first place. You can actually do all these modifications (change the event, change the URL, and even delete the whole behavior) through the Actions panel. You get the same results either way.
You can expand the set of behaviors installed on your machine. Actually, it's not terribly difficult to make your own. A behavior is really just a template of code. It gets a bit more involved when you define the dialog boxes that pop up, but it's all fairly straightforward. My point here is that you might not like the set of behaviors that ships with Flash at this point, but you'll surely see more over time.
Behaviors are simply a tool that guides you through ActionScript. But they can become more trouble than they're worth, especially when you know exactly what you want to do. Sort of like cookie cutters, they're great for holidays, but sometimes you just have to use your fingers and shape the cookie yourself. However, there are some really great benefits to behaviors, too, as discussed in the following sections.
Addressing Movie Clips
The navigation actions you saw in the preceding section are good for jumping around within a Timeline or throughout the Web. However, you know that movie clips have their own Timeline. What happens when you want to jump around within a movie clip? If you put an action inside the movie clip or if you attach an action to the clip, it's pretty straightforward. If you have a stop action on a button inside the movie clip, for example, it will cause the movie clip to stop (provided that it has multiple frames). So scripting is easy when you put an action in the master clip or on a clip instance.
Your job gets a little more complicated when you want to send an action to another clip remotely. For example, say you have a movie clip and a button on the Stage (the button is not inside the movie clip). If you put a stop action on the button, it will cause only the main Timeline to stop; the movie clip won't stop. To direct an action to a clip, you first address, or "target," that particular instance of the movie clip (remember, you could have several instances on the Stage at once). You can do this in Flash in several ways.
Consider that you have to do two things: address the clip and tell it what to do. Remember from Hour 12, "Animating Using Movie Clip and Graphic Symbols," that instances of movie clips can be named (via the Properties panel). You can only address named clip instances. So addressing a clip is simply stating its name. That is, you don't say stop() but you say clip.stop() (where clip is the instance name of the clip you want to stop). If the clip is nested inside another clip, you must address its entire path (or full address). You place the action immediately following the clip's address.
It might be easiest to understand this by using pseudo-code. You can use the programmer's trick of writing scripts in your own words being as clear as possible just to get things sorted out. Then you translate to real code. For example, if you wanted a clip instance named ball to stop playing, you might say (in pseudo-code): "ball, you stop." In English, you might say "stop ball," but remember that you have to address the object first and then tell it what code to execute.
The syntax to address an object uses what's called dot syntax. For example, this code would actually make a clip instance named ball stop playing:
If that sounds simple, then you've got about one-third of ActionScript under your belt. In the following task, you can practice addressing.
Try It Yourself: Target Nested Instances
In this task you'll address and stop instances of wheels that are inside a clip of a car:
Addressing clips is simple, as long as you remember to name your instances. (Just make sure each instance name is just one word and does not begin with a number.) Using the Insert Target Path dialog box is a nice way to learn the syntax for addressing. When you choose the clip from the hierarchy, Flash automatically puts it in the correct syntax. Addressing clips goes from the general to the specific, as in this.the_car.back_wheel, but until you know these conventions, it's probably safest to use the Insert Target Path dialog box.
Action Efficiency Tricks
When you have an idea how to insert actions, you can work on fine-tuning the technique. Besides the general suggestion to be deliberate and include no more actions than necessary, there are a few more specific ways to be efficient.
The first technique is to use a separate layer for all frame actions, as you did in the first task in this hour. You can put an action in any keyframe, but sometimes you want an action to execute when the playback head reaches the middle of a tween. Instead of inserting a keyframe (and effectively messing up the tween), you can use a separate layer to insert all new keyframes. Not only will this prevent tweens from breaking, but you'll have only one layer in which to manage your frame actions. Otherwise, you might be hunting through all your layers looking for actions.
Another similar technique is to use a separate layer for all frame labels. In the second task in this hour, you saw the beauty of using labels as destination points for the goto actions. Just as you don't want to restrict the design of your tweens to accommodate frame actions, it's nice to keep all your labels in a layer of their own. There's no reason to be stingy with layers insert them as needed. Layers don't affect file size, and they can help organize your movie.
There are many more efficiency tricks, but these are just a few that apply to actions. You'll learn more throughout the rest of the book.
|< Day Day Up >|