Choosing a Button s Event Handler

I l @ ve RuBoard

Choosing a Button's Event Handler

When you click a basic Flash button, pressing the mouse button down is what actually triggers Flash to display the Down frame. When you attach ActionScript to a button, the script adds to the button's built-in actions. In Normal mode, when you at ach an action to a button 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 change that behavior by selecting a different event parameter for the handler.

In the preceding exercise, you let Flash add the event handler automatically when you chose an action. This time, try choosing the event-handler action yourself.

To choose the triggering mouse events:

  1. Open a new copy of the ObjectActions Template that you created in "To set up a document for testing actions" earlier in this chapter (a movie with five keyframes and a stop action in Frame 1).

  2. In Keyframe 1, select the button instance on the Stage.

  3. Access the Actions-Button panel.

  4. From the Add menu, choose Actions > Movie Control > on (Figure 13.22).

    Figure 13.22. The on action allows you to define a button's triggering mouse event.

    graphics/13fig22.gif

    Flash adds the event handler on (release) and a pair of curly braces to the Script pane. The parameters gov-erning mouse events appear in the parameters area. These parameters specify the exact mouse event that will trigger any actions that you add between the handler's curly braces.

  5. In the Event section of the parameters area, uncheck the Release checkbox.

  6. Check one or more of the other Event checkboxes (Roll Out, for example).

    Flash updates the Script pane, adding the specified mouse events within the parentheses. To set multiple parameters for a mouse eventsuch as making the button respond to a click within the button area ( On Press ) as well as to the user 's rolling the pointer out of the button area ( On Roll Out )check more than one checkbox (Figure 13.23).

    Figure 13.23. With the on action selected in the Script pane, you can select one or more triggering mouse events for a button in the parameters area of the Actions panel.

    graphics/13fig23.gif

  7. With on (press, rollOut) { selected in Line 1 in the Script pane, from the Add menu, choose Actions > Movie Control > goto .

  8. In the parameters area, from the Type pop-up menu, choose Next Frame. Flash adds nextFrame (); to the Script pane between the curly braces (Figure 13.24).

    Figure 13.24. With this script, in which Press and Roll Out are selected as the event parameters, your viewers can trigger the nextFrame action in two ways: by clicking inside the button area or by rolling the pointer into and then out of the active button area.

    graphics/13fig24.gif

    The Go To and Play and Go To and Stop radio buttons are grayed out. The nextFrame action instructs the button to take the viewer to the next frame and then stop playback.

    You're ready to see the button in action.

  9. Choose Control > Test Movie.

    Flash exports the movie and opens it in Flash Player. The button responds to the pointer's rolling into and then out of the button area by taking you to Frame 2 of the movie. Flash also responds to a downward press of the mouse button within the button area by taking you to Frame 2.

graphics/01icon02.gif Tips

  • Compare the two processes you've used for adding ActionScript to a button. In the first exercise, you started your script by choosing a goto action; because you are working in Normal mode, Flash assisted you by adding the necessary event handler to your script. In the second exercise, you chose the event handler on your own and also chose the action. In addition, you modified the handler so that the button would respond to different user activities. Compare the resulting scripts. They are quite similar; only the Event parameters of the handler differ (Figure 13.25).

    Figure 13.25. Whether you let Flash assist you with adding event handlers or you select them yourself, the resulting scripts will carry out the same instructions. The top script was created by selecting the goto action and setting its parameters. The bottom script was created by selecting the on action, changing the Event parameters of the on action, and then selecting a goto action and setting its parameters.

    graphics/13fig25.gif

  • You do not have to choose the event handler from the Actions Toolbox or Add menu to change its parameters. When you are using Normal mode and select an action, Flash adds the handler for you. Simply select the handler in the Script pane. The parameters area displays the Event choices.


Terminology: Mouse Event Parameters

Buttons can respond to eight different events. Because all but one of these events involve user input with a mouse (or equivalent device), they are called mouse events . You can choose which mouse event triggers your button'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 button by 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. 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 has not been pressed.

Roll Out happens any time the pointer rolls out of the button's hit area when the mouse button has not 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.

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.

I l @ ve RuBoard


Macromedia Flash MX for Windows and Macintosh. Visual QuickStart Guide
Macromedia Flash MX 2004 for Windows and Macintosh (Visual QuickStart Guides)
ISBN: 0582851165
EAN: 2147483647
Year: 2002
Pages: 243

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