Using the Actions Panel

     

You enter all your ActionScript in the Actions panel. ActionScript can be attached to a frame in a timeline, or to a button, movie clip, or component on the Stage. To attach the script to the correct item, click on the frame, button, movie clip, or component before starting to type in the Script pane on the right side of the Actions panel.

Adding a behavior just automates this process, as you can confirm by adding a behavior and then ”without deselecting the frame or object with the behavior ”opening the Actions panel, if it's not already open . You'll see the ActionScript for the behavior.

On the left side of the Actions panel is the Actions toolbox. (There's also a Script Navigator below the Actions toolbox, which represents the structure of your FLA file in a hierarchical format.) The Actions toolbox is like an ActionScript dictionary, except that it is organized functionally (by the way you use each item), rather than alphabetically . You can save keystrokes by dragging and dropping items from the Actions toolbox into the Script pane. Alternatively, right-click on any entry in the Actions toolbox, and select either Add to Script to copy that item into the Script pane, or View Help to bring up the help file (if one exists) for that item. Clicking the Reference button (refer to Figure 19.1) is another way to bring up the help file.

Selecting Frame Actions or Object Actions

A given block of actions can be attached either to an object (button, movie clip, or component) or to a frame. When you click on an object or a frame, the title bar at the top of the Actions panel changes to say "Actions ”Frame," "Actions ”Movie Clip," "Actions ”Button" or just "Actions" (for a component).

In general, frame actions execute when the playhead reaches the frame to which the actions are attached. Object actions, on the other hand, are executed when a designated event occurs involving that object. For instance:

  • Frame action . Suppose you put a trace action in a frame, such as

     trace("hello"); 

    The word hello will be displayed in the Output window each time the playhead reaches that frame.

  • Object action . Movie clips have an "enter frame" event that occurs when the movie clip is on the Stage and the playhead enters a frame. So, for example, if your movie is running at 12 frames per second (fps), the "enter frame" event for that movie clip occurs 12 times per second when the movie clip is on the Stage. You can "hook into" this event by creating an event handler for it. Any ActionScript code that you put in your event handler will execute every time the event occurs. An "enter frame" event handler attached to an object looks like this:

     onClipEvent(enterFrame) {      // your code goes here! } 

In general, the ActionScript in a frame executes when the playhead reaches that frame. There are two notable exceptions, however:

  • A function is a named block of code that implements a particular procedure. For instance, gotoAndPlay() is a built-in movie clip function that causes a movie to go to a particular frame in its Timeline and start playing.

    You can define your own custom functions anywhere that you can write code ”within a frame or within an event handler. When the playhead reaches a frame or executes an event handler containing a custom function definition, the function becomes defined and available, but it does not actually execute. You have to call the function to cause it to execute.

    For more information on functions, see "Combining Statements into Functions," page 486 , in Chapter 20, "Basic ActionScript."


  • In addition, there is another format for event handlers that allows you to enter object event handlers "in-line" with other code in a frame, rather than attached to an object. For instance, the following "enter frame" event handler is attached to a frame, but it works as if it were attached to the movie clip named myClip :

     myClip.onEnterFrame = function () {      // your code goes here! }; 

    When the playhead reaches a frame where an in-line event handler is defined, Flash starts "listening" for the event. However, like an attached event handler, the in-line event handler fires (executes the code it contains) only when the event occurs. With the "enter frame" event, this is likely to be immediate. However, there are other events, such as onRollOver() (the mouse rolling over the object) which might never occur at all.

    Note that you cannot successfully assign an in-line event handler to an object unless the object exists in the workspace in the frame where you define the event handler. (The object could be in the offstage area, and thus not visible.) You can put the code in the frame, but it has no effect.

In the preceding example, myClip is the instance name of a movie clip. Movie clip instances are covered in Chapter 18, "Animation, Interactivity, and Rich Media," in the "Symbols, Instances, and Library Assets section, page 380 .


Event handlers that continue to fire when theyre not performing any useful function can slow down your program. If you want to disable an event handler, see "Disabling and Deleting Event Handlers," page 507 , in Chapter 20, and disableHandler.htm on the CD accompanying this book.


Hiding and Un-hiding the Actions Toolbox

You can hide the Actions toolbox, thus gaining more space for the Script pane, by clicking the triangle between the Actions toolbox and the Script pane. (Refer to Figure 19.1.) To un-hide the Actions toolbox, click the triangle again. (You can collapse the panels on the right side of the screen similarly by using the triangle on that side.)



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

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