|
Free Open Book
Macromedia Flash 8 Bible |
Actions and Event HandlersEven the most complex interactivity in Flash is fundamentally composed of two basic parts: the behavior (what happens), and the cause of the behavior (what makes it happen). Here's a simple example: Suppose you have a looping soundtrack in a movie and a button that, when clicked, turns the soundtrack off. The behavior is the sound turning off, and the cause of the behavior is the mouse clicking the button. Another example is stopping an animation when it reaches a certain frame on its timeline. When the last keyframe of the animation is played (the cause), an action on that keyframe stops the animation (the behavior). In Flash, the building blocks of behaviors are referred to as actions.
The first step in learning how to make interactive movies is becoming familiar with the list of possible actions. However, actions can't act without being told to act by something. That something is often the mouse pointer coming in contact with a button, but it can also be a keystroke, or simply a command issued from a keyframe. We refer to any occurrence that can cause an action to happen (such as the button click in the preceding example) as an event. The mechanism you use to tell Flash what action to perform when an event occurs is known as an event handler. This cause-and-effect relationship seems obvious, but it is an extremely important concept. For the purposes of creating basic interactivity, the difference between an action and the cause of an action is merely a practical detail. As the set of Flash actions, collectively know as ActionScript, continues to grow with each release of the Flash authoring tool (and, therefore, the interactive capabilities that they provide), understanding the relationship between actions and the things that cause them can be the key to adding more sophisticated behavior to your movies with traditional programming techniques. Every interactive framework, whether it is Macromedia Flash or Macromedia Director or Apple DVD Studio Pro, has unique handlers for specific events. Table 18-1 relates interactive events with Flash handlers.
While the breadth and depth of ActionScript involved with the interactions described in Table 18-1 may seem overwhelming, don't worry — we're taking it one step at a time. First, you'll learn about what the new Behaviors panel can do. Then you'll learn how to set up the Actions panel, whose look and feel has changed from previous versions of Flash. Later, we'll look at actions that control movie playback. Later, you'll also learn how to call these actions in various ways with three kinds of event handlers: button manipulation, keyframes, and keystrokes. What are Behaviors?Flash 8 includes a Behaviors panel, which was introduced in Flash MX 2004. This panel is designed to help a novice who is just starting to use Flash for design and development. You can open the Behaviors panel, shown in Figure 18-1, by choosing Window ð Behaviors (Shift+F3). If you create a new Flash document and select the first frame of Layer 1, you can click the Add Behavior (+) button at the top-left corner of the Behaviors panel to see a list of categories (see Figure 18-2). These categories represent a variety of objects you can control with one or more behaviors. So what do behaviors do? In the simplest sense, a behavior is automated scripting. When you add a behavior to an event handler, Flash 8 generates the necessary ActionScript code to make that behavior happen. Let's take a quick look at a Web behavior.
We won't discuss any other behaviors in this chapter, as you can find specific categories of behaviors explained in other parts of the book:
Behaviors enable you to learn by example — you can add a behavior to an object and look at the code used to describe the interaction.
What is ActionScript?Every interactive authoring system uses a language (or code) that enables elements within the system to communicate. Just as there are several languages that people use to speak to one another around the globe, there are hundreds of programming languages in use today. In an effort to make Flash more usable to computer programmers, Flash's scripting language, called ActionScript, changed much of its formatting in Flash 5 to mirror JavaScript, a fundamental element for Dynamic HyperText Markup Language (DHTML) and HTML Web pages. Right now, we focus on using the most basic Flash ActionScript.
Navigating the Actions PanelFlash 8 has a specific interface element that enables you to add interactive commands to Flash movies — the Actions panel. Unlike behaviors and the Behaviors panel, you don't use menus to add interactive functionality — you type the ActionScript code describing the interactivity in (or out of) of the Actions panel. You can open the Actions panel in a number of ways:
If you have a keyframe selected in the Timeline window, the Actions panel will be titled Actions - Frame (see Figure 18-5). If you have a Movie Clip symbol instance selected on the Stage, you'll see the name Actions Movie Clip. If you have a Button symbol instance selected on the Stage, the Actions panel will be titled Actions - Button. If you have a component selected, the Actions panel will simply read Actions. Don't be confused — there is only one Actions panel. Flash 8 simply lets you know the object to which you are assigning actions.
Figure 18-5: The Actions panel enables you to instantly add, delete, or change Flash movie commands.
As shown in Figure 18-5, the Actions panel in Flash 8 has three distinct areas (counter-clock-wise from the left): the Actions toolbox, the Script navigator, and the Script pane. The new Script Assist area is available within the Script pane. There are two auto-snap dividers, one dividing the Actions toolbox and Script navigator from the Script pane, and another subdividing the Actions toolbox and Script navigator. You may want to practice opening and closing these dividers, as well as dragging each to your preferred width and height, respectively. Figures 18-5 and 18-6 show a breakdown of the new Actions panel in Flash 8.
Figure 18-6: The Script Assist mode displays a more user-friendly interface for controlling action parameters.
For this chapter, you'll work primarily within the Timeline Control booklet, located within the Global Functions booklet in the Actions toolbox. You can add actions to the Script pane in one of three ways:
To delete actions, select the action line(s) in the Script pane, and press the Delete or Backspace key on the keyboard. Once you have added an action to the Script pane, you can specify parameters (or arguments) for the action. Depending on the action, you may or may not need to type such parameters. By default, Flash 8 provides code hints as you type actions into the Script pane. The Show Code Hint button enables you to see the parameters for an action, as shown in the gotoAndPlay action in Figure 18-5. More importantly, the Script Assist mode makes it much easier for you to add and adjust settings for basic actions, especially those you find in the Timeline Control booklet. You use the Script Assist mode throughout this chapter to learn the basic control actions. You should get in the habit of clicking the Check Syntax button (the blue check mark) to make sure you didn't mistype an action. If you have an error, the Output panel displays some information related to the error, indicating the line number where the syntax error occurs.
For now, let's look at two booklets in the Actions toolbox's Global Functions booklet: Timeline Control and Browser/Network. The Timeline Control actions are listed in alphabetical order. The first eight actions in this booklet, including gotoAndPlay, gotoAndStop, play, and stop, control the playback of the movie. The last action, stopAllSounds, is a global command to handle sound playback.
The Browser/Network actions — fscommand, getURL, loadMovie/loadMovieNum, loadVariables/loadVariablesNum, and unloadMovie/unloadMovieNum — enable movies to load external files and communicate with the browser, a Web server, or the stand-alone player. In this chapter, we'll get you up and running with the getURL action, which enables you to link to other Web resources outside of the Flash movie (such as Web pages and file downloads).
The remaining Action booklets primarily offer extended ActionScript programming capabilities. We discuss many of these actions in later chapters. A Brief Primer on Code SyntaxSome of the most difficult concepts for beginners to understand with code writing are white space, parentheses (()), semicolons (;), and curly braces ({}). In the following paragraphs, you learn how each of these affects your ActionScript code. White SpaceWhite space is a collective term referring to any blank areas between lines of code. It includes spaces inserted by the spacebar, indentations inserted with the Tab key, and line returns inserted with the Enter or Return key. When Flash 8 compiles your ActionScript code into the Flash movie, the white space between your lines of code usually will not generate any errors. For example, the following code works exactly the same:
on(release){ getURL("mypage.html"); }
or
on(release){
getURL("mypage.html");
}
or
on ( release){
getURL("mypage.html");
}
However, white space is an issue when it separates the key terms in the action, such as:
get URL("mypage.html");
The space between get and URL will cause an error when Flash 8 tries to create the Flash movie.
ParenthesesMany, but not all, actions require parentheses after the action term, such as on(), getURL(), or even play(). A general rule to remember is that if the action requires a parameter, such as on (release), then parentheses are required as well.
However, many actions, such as play() and stop(), still require parentheses, even though they do not use any arguments. Another rule for parentheses is that any open parenthesis (() must eventually be followed by a closing parenthesis ()). A habit we like to encourage is counting the number of opening parentheses in a script and then counting the number of closing parentheses. If the numbers don't match, you need to review your code to find the place where you forgot to include a parenthesis. Semicolons and Curly BracesYou've probably already noticed that most actions include a semicolon (;) at the end of the code line. In practice, many coders forget to include semicolons. Usually, Flash is very forgiving if you omit semicolons, but by no means should you be encouraged to omit them. The general rule for semicolons and curly braces is mutually inclusive: If your action doesn't end with an opening curly brace ({), it should end with a semicolon. As with parentheses, all opening curly braces must eventually be followed by a closing curly brace (}). Curly braces are commonly used with actions beginning with on, such as on() and onClipEvent(), as well as if and function declarations.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |