Macromedia Flash 8: Training from the Source

You saw how to use a behavior to load in an external JPEG image in the previous exercise, although you haven't checked to see whether it works yet. In this example, you will learn how to use one of the behaviors in Flash to open a web page in a new browser window. You will be adding the behavior directly to a movie clip instance instead of to a frame in the Timeline this time. This is sometimes referred to as an object action.

You should still be using bookstore10.fla for this exercise.

1.

Make sure that you are on the main Stage and then select Frame 1 of the home layer. Using the Selection tool, click the instance of mcMMPressLogo that you created in the previous exercise. There is an action in Frame 1 of the actions layer that references this instance, but you are going to place an action on the movie clip itself so that if a user happens to click it, it will launch a web page.

2.

Add a behavior directly to the mcMMPressLogo movie clip instance using the Behaviors panel.

With mcMMPressLogo selected, click the Add Behavior button in the Behaviors panel and choose Web > Go to Web Page from the menu. The Go to URL dialog box appears and gives you the option to select a target: _self, _parent, _blank, or _top. Each of these options is the same as the HTML counterparts; change the value to _blank.

3.

Set the target URL for the behavior in the Go to URL dialog box.

Setting the target URL to redirect to is as simple as replacing the default value in the URL text field with your desired target. For this exercise, set the target URL to: http://www.peachpit.com. After you type in the URL, press OK and return to the Stage.

Note

If you are trying to go to a web page within a different domain, don't forget to prepend your URL in the Go to URL dialog box with http://. Modern browsers are making us lazy because we no longer have to type http:// into the address window to go to a web page these days. When you're developing or setting a link to another page, however, you absolutely must specify the protocol, which in this circumstance is Hypertext Transfer.

4.

Change the behavior's triggering event using the Behaviors panel.

Click the mcMMPressLogo instance on the Stage. You can alter when the behavior executes by opening the Behaviors panel and changing some settings. Open the Behaviors panel and look at the Event and Action lists below the Add Behavior and Delete Behavior buttons.

If you click the On Release text to the left of the Go to Web Page action, you will see a drop-down list of events to trigger this behavior on. By default, the event is On Release, which means the user presses and releases the mouse button over the mcMMPressLogo movie clip. You can choose a new value from the drop-down list and trigger when the user moves his or her mouse cursor over the instance, or when the mouse cursor moves off the instance entirely. You can also cause the event to trigger when a certain key is pressed. Change the event to a Key Press event. Flash prompts you to press a key on the keyboard that will be used to open the Peachpit website in place of a mouse click. Change the event back to On Release because this is the interaction that people are used to.

5.

Modify the behavior properties using the Behaviors panel. Then click OK to apply the changes. From time to time, you might need to change some aspect of the behavior or correct a spelling error in any of the parameters that you had to manually type when you applied the behavior to a frame or object. Lucky for you, you can get back in to the behavior to fix it or change it at any time you like from the Behaviors panel. If you want to modify the behavior and redirect the user to a different URL, you can modify the behavior by double-clicking it in the Actions column on the Behaviors panel. This will pull the dialog box back up that you used to set the URL. Make any changes that you like and press OK.

Tip

You can also edit the ActionScript that the behavior added directly in the Actions panel. You haven't really used the Actions panel so much yet, and because you haven't learned ActionScript basics at this point, it's not advisable to try. After you've learned a little more, though, it sometimes is the smartest alternative to changing how a behavior operates.

Категории