|
Free Open Book
Macromedia Flash 8 Bible |
Making Actions Happen with Event HandlersThe ten common actions we discussed in the previous sections provide many of the behaviors that you need to make an interesting interactive Flash movie. But those actions can't make your movies interactive on their own. They need to be told when to happen. To tell a Flash movie when an action should occur, you need event handlers. Event handlers specify the condition(s) under which an action can be made to happen. For instance, you might want to mouse-click a button to initiate a play() action, or you might want a movie to stop when a certain keyframe in the timeline is reached. Creating interactivity in your movies is simply a matter of deciding what event you want to detect (mouse click, keystroke, and so on), adding the appropriate event handler to detect it, and specifying the action(s) that should be performed when it happens. Before we describe each event handler in detail, we'll show you an example of exactly how an event handler merges with an action to form a functioning interactive button. Combining an Action with an Event Handler to Make a Functioning ButtonImagine that you have a short, endlessly looping movie in which a wire-frame cube rotates. Now imagine that you want to add a button to your movie that, when clicked, stops the cube from rotating by stopping the playback of the looping movie. Here's what you need to do:
To make any interactivity in your movies, you simply have to apply the basic principles you used to make the stop button: Decide which action (or actions) you want to happen, and indicate when you want that action to happen with an event handler. Let's look now at more event handlers you can use to make those actions happen. The Flash Event HandlersThree primary event handlers exist in Flash: those that detect mouse activity on Button instances (button manipulation), those that recognize when a key is pressed on the keyboard (key presses), and those that respond to the progression of the timeline (keyframes). Working with Mouse Events and ButtonsEvent handlers that occur based on the user's interaction with a button rely entirely on the location and movement of the mouse pointer. If the mouse pointer comes in contact with a Button symbol's Hit area, it changes from an arrow icon to a finger pointer icon. At that time the mouse is described as "over" the button. If the mouse pointer is not over a button, it is said to be out or outside of the button. General movement of the mouse without the mouse button depressed is referred to as rolling. General movement of the mouse with the mouse button pressed is referred to as dragging.
It's worth mentioning that you can assign one or more mouse events to a Button instance's on handler. For example, the following code will be invoked when the user releases the mouse button over or outside of the Hit area of the Flash button:
on (release, releaseOutside){
stop();
}
As this code demonstrates, multiple events are separated by a comma (,). You can specify as many events as you need within the parentheses of the on handler. Here are the mouse-based events for Flash buttons: PressA single mouse click can actually be divided into two separate components: the downstroke (the press) and the upstroke (the release). A press event occurs when the mouse pointer is over the Hit area of a button and the downstroke of a mouse click is detected. Press is best used for control panel — style buttons, especially toggle switches.
ReleaseA release event occurs when the mouse pointer is over the Hit area of a button and both the downstroke and the upstroke of a mouse click are detected. The release event is the standard button click event.
ReleaseOutsideA releaseOutside event occurs in response to the following series of mouse movements:
The releaseOutside event can be used to react to an aborted button click. RollOverA rollOver event occurs when the mouse pointer moves onto the Hit area of a button without the mouse button depressed.
RollOutA rollOut event occurs when the mouse pointer moves off of the Hit area of a button without the mouse button depressed. This event is commonly used for switching an advanced button's graphic state back to its original state when the user rolls off the button. DragOverA dragOver event occurs in response to the following series of mouse movements:
The dragOver event is rather obscure, but you could use it for special cases of interactivity such as revealing a hidden item in a game. For example, when the mouse button is held down and mouse movement occurs over a specific area, ActionScript can detect the coordinates of the mouse movement and reveal a Movie Clip instance that is otherwise invisible on the Stage. DragOutA dragOut event occurs in response to the following series of mouse movements:
As with dragOver, you'll likely encounter very few situations where the dragOut event is necessary. Most of the more complicated mouse events are only useful for Flash games and experimental user interfaces. Capturing Keyboard InputYou can also use event handlers to detect events that occur on the user's keyboard. You can enable your Flash movies to "capture" a key press (also known as a keystroke) initiated by the user. One way in which ActionScript can detect a keystroke is by using the keyPress event. This event lets you execute an action (or series of actions) when the user presses a key on the keyboard. The implementation method for a keyPress event handler may be confusing, but it's the least code-intensive (and most designer-friendly) route: To add a keyPress event handler, you must first place a button onstage at the frame where you want the keyboard to be active. You then assign the keyPress event to the Button instance's on handler. Keep in mind, though, that the button's Hit area has no effect on the keyPress event detection. As such, even though the keyPress event is defined on a button, any key press that occurs in the Flash movie can be captured by the button, regardless of the user's mouse position.
For example, if you have a Button instance on the Stage of your Flash document, you can select the Button instance, open the Actions panel, and add the following code to capture an Enter keystroke:
on (keyPress "<Enter>"){
trace("The Enter key was pressed.");
}
As you can see in this example, you specify the key's name between a set of double quotes, after the keyPress term. Some keys, such as Enter and Escape, require less than (<) and greater than (>) characters as well. You can use the keyPress event in conjunction with other mouse events. The following example detects when the user clicks the mouse button over the Hit state of the Flash button or presses the spacebar anywhere within the Flash movie:
on (release, keyPress "<Space>"){
stop();
}
The keyPress event, which was introduced with Flash Player 4, and the newer Key object, introduced with Flash Player 6, open up many possibilities for Flash. Movies can have keyboard-based navigation, buttons can have keyboard shortcuts for convenience and accessibility, and games can have keyboard-controlled objects (such as ships and animated characters). But watch out for some potential "gotchas" to keyboard usage, specifically with on handlers and keyPress events. If you're planning ambitious keyboard-based projects, you may want to check the following list of potential issues first:
Capturing Time Events with KeyframesThe keyframe event handler depends on the playback of the movie itself, not on the user. Just about any action (except the on() and onClipEvent() handlers) can be attached to any keyframe on the timeline. An action attached to a keyframe is executed when the Playhead enters the keyframe, whether it enters naturally during the linear playback of the movie or as the result of a goto action. So, for instance, you may place a stop action on a keyframe to pause the movie at the end of an animation sequence. In some multimedia applications, keyframe event handlers can differentiate between the Playhead entering a keyframe and exiting a keyframe. Flash has only one kind of keyframe event handler (essentially, on enter). Hence, as a developer, you do not need to add keyframe event handlers explicitly — they are a presumed element of any action placed on a keyframe. As we mentioned in an earlier note, ActionScript 1.0 and 2.0 can employ a more advanced event model. You learn about different event models in Chapter 25, "Controlling Movie Clips."
The process for adding an action to a keyframe is as follows:
In the next section, you'll get a little more hands-on experience adding actions to both buttons and keyframes. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |