Macromedia Flash 8 for Windows & Macintosh

When you preview the three states of a basic button symbol during authoring (choose Enable Simple Buttons), pressing the mouse button down triggers Flash to display the Down frame. You can use ActionScript to override a button's built-in actions. When you use Script Assist or behaviors to attach actions to a button-symbol instance, Flash automatically adds the default event handler, on (release), to the Script pane. That code makes the release of the mouse button trigger the actions attached to the button. You can select different triggering events in the Script Assist window. (If you created the script using behaviors, you can also choose a new event handler in the Behaviors panel.)

To choose a triggering event using Script Assist

1.

Open the document GoToButtons.fla, which you created earlier in this chapter.

This is a five-frame movie with a button symbol and a button component that you scripted to take you to different frames when clicked.

2.

On the Stage, select the button-symbol instance and duplicate it.

Choose Edit > Duplicate or copy and paste to create a duplicate. To help you remember which button is which, place identifying text near each button. Label the original button Release; label the duplicate Press.

3.

Select the Press button.

Its script appears in the Script pane in the Actions panel.

4.

Make sure Script Assist is active; if the Script Assist window isn't visible, click the Script Assist button.

5.

In the Script pane of the Actions panel, click anywhere in line 1, the line containing the event handler on (release).

Flash highlights the line of script and displays a set of check boxes for assigning triggering events in the Script Assist window (Figure 13.24).

Figure 13.24. When you select a line of script in the Script Pane, the Script Assist window displays other possible options for the selected action. With line 1 (the event handler) selected, the Script Assist window displays other possible triggering events.

6.

Deselect the Release check box.

7.

Select the Press check box.

The Script pane updates to reflect the new event choice (Figure 13.25).

Figure 13.25. The Script pane updates to reflect your choices in the Script Assist window. Here line 1 of the script now uses the on (press) event handler.

8.

Choose Control > Test Movie to see these buttons in action.

Position the pointer over the original button (labeled Release); click and hold for a second or two, and then let go of the mouse button. Flash moves to frame 3 only after you release the mouse button. Now return to the first frame where your buttons are (choose Control > Rewind). Position the pointer over the Press button. Click and hold. Flash takes you to frame 3 immediately; you don't need to release to activate the script.

Tip

  • If you want the button to respond the same way to multiple triggering events (for example, when the user rolls over or rolls out), select the event check boxes for those events. The triggering events all appear within the parentheses defining the event parameter of the action: for example, on (roll over, roll out).

To choose a triggering event using the Behaviors panel

1.

Continuing with the document GoToButtons.fla, on the Stage, select the button-component instance and duplicate it.

Place identifying text near each component instance. Label the original click; label the duplicate keyDown.

2.

Select the button component labeled keyDown.

Its script appears in the Script pane in the Actions panel. The component's behavior also appears as an entry in the Behaviors panel.

3.

In the Behaviors panel, click the first line in the Event column (the word click).

Flash activates a menu of events.

4.

To view the event choices, click the triangle to the right of the word click.

A menu of events appears (Figure 13.26).

Figure 13.26. Click the entry in the Event column for a behavior to activate a list of events (top). Click the active field or the triangle to its right to expand the list (bottom).

5.

From the menu of events, select keyDown.

The value in the Events column of the Behaviors panel changes to keyDown, and the script in the Actions panel updates (Figure 13.27). (Note that keyDown is intended to be used with more advanced ActionScripting techniques, where you would write a listener script to capture the exact keys being pressed. But you can use keyDown here to practice changing an event through the Behaviors panel.)

Figure 13.27. Choose a new event to modify the selected behavior. Flash updates the script in the Actions panel accordingly. With on (keyDown) as the event handler, this component carries out its task when a user presses any keyboard key.

6.

Choose Control > Test Movie to see the two button components in action.

You must click the component labeled click to get it to carry out its task of taking you to a different frame. Clicking the component labeled keyDown does nothing; but press any key while that component is on Stage, and Flash whisks you off to frame 4.

Tips

  • If you use the Behaviors panel to assign actions to button symbols, use the same steps as in the preceding task; the event choices in the menu in step 5 will be appropriate to whatever type of object you're working with.

  • Although you created the button-component script in the Behaviors panel, you can edit the script in the Actions panel using Script Assist. In the Script pane, select line 3, the line containing on (click) or on (keyDown). In the Script Assist window, make sure the Component check box is selected. This option tells Flash to treat the object as a component. Choose the desired event from the pop-up menu (Figure 13.28). At this level of editing, Flash keeps the link to the behavior, updating the Event column in the Behaviors panel. If you make substantial edits to a behavior-created script in the Actions Panelespecially if you edit the script directlywithout Script Assist, you may lose the ability to edit the script via the Behaviors panel.

    Figure 13.28. You can update behavior-created scripts in the Actions panel using Script Assist. In the Script pane, select the line of code containing the event; in the Script Assist window, select the Component check box; and then choose an event from the pop-up menu.

The Mystery of Mouse Events

For button symbols and movie clips, the event handler on() can respond to eight different events. Because most of these events involve user input with a mouse (or equivalent device), they're often called mouse events. You can choose which mouse event triggers your button or movie clip's script:

Press refers to the downward part of a click when the pointer is located within the hit area of a button.

Release refers to the upward part of a click (the user presses and then releases the mouse button) when the pointer is located within the hit area of a button. A Release event lets users click and then change their mindsand avoid activating the buttonby dragging away before releasing the mouse button. This is the way most buttons in professional programs work.

Release Outside happens when the user clicks inside the button area, holds down the mouse button, and moves the mouse outside the active button area before releasing the mouse button.

Key Press happens any time the user presses the specified keyboard key while the Flash button is present on screen. The user doesn't have to use the mouse to interact with the button for this event to trigger an action.

Roll Over occurs any time the pointer rolls into the button's hit area when the mouse button hasn't been pressed.

Roll Out happens any time the pointer rolls out of the button's hit area when the mouse button hasn't been pressed.

Drag Over works in a slightly unexpected way. A Drag Over event occurs when the user clicks and holds down the mouse button within the button's hit area, rolls the pointer outside the hit area, and then rolls the pointer back into the hit area, all without releasing the mouse button.

Drag Out happens when the user clicks within the button's hit area, holds down the mouse button, and rolls the pointer out of the hit area.

There are ten events for components (click, focusIn, focusOut, keyDown, keyUp, resize, move, draw, load, and unload). The one that makes the most sense for a button component is click, which is the equivalent of Release for button symbols. The other events are meant for use with more-advanced ActionScripting, where the script gathers information about what transpires: for example, keeping track of which keys the user presses.

Категории