Using Behaviors

 < 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:

  • Add Behavior You always select a behavior to add by clicking the Add Behavior (plus) button. This reveals a hierarchical menu of all installed behaviors.

  • Delete Behavior The Delete Behavior button lets you remove a behavior. Alternatively, you can just select and delete any row containing a behavior that you want to remove.

  • Move Up and Move Down These buttons let you reorder multiple rows of behaviors (you can add more than one).

  • Event The Event drop-down list lets you specify a trigger for any added behavior. Remember that when attaching actions to objects, you have to specify events such as press or release.

  • Action column The Action column simply presents the name of any added behavior. In addition, you can double-click any row in this column to repopulate a behavior after it's added.

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:

1.

In a new file, create a Button symbol called myButton and place an instance on the Stage. Give this instance the name go.

2.

With the button instance selected, open both the Actions panel (by pressing F9) and the Behaviors panel (by pressing Shift+F3). Move the Actions panel to the side because you're only going to use it to watch what's happening behind the scenes.

3.

Make sure the button is selected by ensuring that you see myButton, <go> in the Behaviors panel (as shown in Figure 15.12).

Figure 15.12. When you select the button, you see its symbol and instance name in the Behaviors panel.


4.

Click the plus button in the Behaviors panel and select Web, Go to Web Page. Into the URL field that appears in the dialog box, type http://www.phillipkerman.com. (Leave the Open In option set to the default, _self.)

5.

Test the movie. Or, better yet, select File, Publish Preview, Default or press F12 so you can watch this in a browser. Just click the button in the Flash movie and, if you're connected to the Internet, you'll hyperlink to my home page.

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:

 ball.stop(); 

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:

1.

You'll need a car with rotating wheels, like the one you made in Hour 12, in the task titled "Use a Movie Clip to Make a Rotating Wheel." Remember that you achieved this by working from the inside out. First, you made a clip of a wheel called Plain Wheel (a circle with lines that would be noticeable when it rotated). Then you used an instance of Plain Wheel to create another clip called Rotating Wheel. Rotating Wheel contained an instance of Plain Wheel in Frame 1 and one in Frame 20. You set a motion tween to rotate the wheel in the first keyframe of Rotating Wheel. Finally, you used two instances of Rotating Wheel in the creation of the car. At this point, either revisit that task in Hour 12 or create the Rotating Wheel clip.

2.

Drag one instance of Rotating Wheel to the Stage. Test the movie to confirm that the wheel is rotating and take note of the direction in which it's rotating.

3.

Drag another instance of Rotating Wheel and place it to the left of the other instance. With the Properties panel, name one of the instances front_wheel and the other back_wheel, as shown in Figure 15.14.

Figure 15.14. Using the Properties panel, name each instance of Rotating Wheel so that each can be addressed individually.


4.

Draw a car body (nothing fancy) around the two wheels. Select everything and choose Insert, Convert to Symbol (or press F8). Name this new movie clip Car.

5.

On the Stage you have an instance of Car, but it has no instance name yet. Use the Properties panel to name this instance the_car. (Note that a clip instance name should have no spaces and should not begin with a number.)

6.

Insert a keyframe at Frame 40 and move the_car to another place on the Stage. In the first keyframe, set motion tweening.

7.

In a new layer, draw a rectangle to be used as a button. Select it and covert it to a symbol called myButton, making sure to set its behavior to Button. Copy and paste this button so that you have two instances. Set the Properties panel's Color drop-down list to Tint and pick a green color for one instance and a red color for the other. Then name the button instances green and red.

8.

Select just the red instance of myButton. Open the Actions panel (confirm that the tab name matches the instance name). Type the following code (either by hand or by dragging pieces from the toolbox):

 on(press){  stop(); } 

9.

Test the movie, and you'll see that the stop button stops the car from moving across the screen, but it doesn't stop the wheels from spinning. You need to add additional actions to stop the wheels.

10.

Back in Flash, access the actions for the red button and add two additional lines of code. Make sure you're not in Script Assist mode and change the code so that it reads as follows:

 on(press){  stop();  the_car.front_wheel.stop(); } 

Because you want the front_wheel instance to stop, and that instance is inside the instance the_car, you need to include that entire path when you address front_wheel. You'll need a third line of code to stop the back_wheel instance, too, but you can insert it a different way. Click right after the semicolon that ends either stop action and press Enter. In order to address the back_wheel instance, click the Insert a Target Path button (it looks like a crosshairs). The Insert Target Path dialog box pops up, with a hierarchy of the named clip instances in your movie. Next to the_car, click the plus sign to see the named clip instances inside it. Click back_wheel and then click OK (see Figure 15.15). Notice that the address this.the_car.back_wheel appears.

Figure 15.15. Using the Insert Target Path dialog box, you can target an individual instance.


11.

Now you have to say what you want to do with the back_wheel instance. The easiest way is to just type .stop(); so that it matches the code that stops the front wheel. The only tricky part is that you must remember to follow stop with parentheses. The finished code appears in Figure 15.16.

Figure 15.16. The code for the stop button includes these three lines of code.


12.

Test the movie. When you click the red button, the car and both wheels stop. You could repeat this process with the play action (that is, play()) on the green button to allow a user to make the car move again.

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 > 


    Sams Teach Yourself Macromedia Flash 8 in 24 Hours
    Sams Teach Yourself Macromedia Flash 8 in 24 Hours
    ISBN: 0672327546
    EAN: 2147483647
    Year: 2006
    Pages: 235

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net