Section 9.2. Adding an Action


9.2. Adding an Action

For an action to work, you must attach it to something in your animation. You add an action to either an object (like a component or symbol) or a frame, depending on when you want Flash to perform the action.

In this section, you see an example of adding an action to an instance of a button symbol (Section 7.1.4), but the technique's the same no matter where you're putting the action. Just remember to select the object on the Stageor a frame in the Timelineand then create the action.


Note: Chapter 10 describes the differences between frame- and object-based actions and when you want to choose one over the other.

To add an action:

  1. Open the document hotspot_drag_begin.fla .

    You can download this example file from the "Missing CD" page at www.missingmanuals.com/cds. It has a drawing all ready for you to practice on. (For a working copy, download hotspot_drag.fla.)

  2. Choose Window Properties Properties to display the Property Inspector .

  3. In the Property Inspector, click the "Instance name " field type in myLeftEye-Instance and then hit Return .

    UP TO SPEED
    Help for Learning ActionScript

    In addition to showing you how to add prebuilt actions called behaviors to your animations, this book gives you working examples of other useful and popular ActionScript actions (including online versions you can download and play with when you get a chance).

    So for a lot of the animations you'll want to create, you're covered. But if you want to create other, more advanced actionssuch as hooking up a component to a back end data source (Chapter 12) or applying a custom data-scrubbing algorithm to a text field your audience types into your Flash formyou'll need to roll up your sleeves and get jiggy with ActionScript.

    Flash itself offers a little help in getting you up to speed on ActionScript, but it's not nearly enough. No mere mortalexcept those with a very strong background in Java, C++, or JavaScriptcan decipher ActionScript from the help Flash provides, which includes:

    • ActionScript documentation . To see Flash's ActionScript documentation, select Help Flash Help. Then, in the Help window that appears, click the arrow next to the Book Categories field and, from the drop-down menu that appears, select ActionScript 2.0 . Two books appear: Learning ActionScript 2.0 in Flash, and ActionScript 2.0 Language Reference (Figure 10-1).

    • The Actions panel . Theoretically, the Actions panel lets you put together actions by pointing and clicking. But while it's true that you can click the Add button to choose an ActionScript function, statement, or property to add to your action, this ability isn't much good if you don't know which functions, statements, and properties to choose (or in which order to choose them). Clicking the ScriptAssist icon tells Flash to display additional script-building hints, but these hints are as good as it gets. (Figure 9-6 has an example.)

    If Flash's ActionScript documentation and the Actions panel don't do it for youand there's a good chance they won'tcheck out the resources listed on Section A.4 of the Appendix. You'll also want to consider a good a book (or three) devoted exclusively to the subject, such as ActionScript for Flash MX 2004 and Essential ActionScript 2.0 , both by Colin Moock (O'Reilly), and ActionScript Cookbook , by Joey Lott (O'Reilly).


  4. Choose Window Actions to display the Actions panel .

    The Actions panel displays both the type of object you selected (Button) and the instance name (myLeftEyeInstance), as shown in Figure 9-5.

  5. In the Actions panel, type the following ActionScript code :

     on (release) {     stopDrag(); } on (press) {     startDrag(this); } 

    Figure 9-4. Because many ActionScript statements require you to refer to a symbol instance by name, you want to get in the habit of always typing in a short, meaningful instance name every time you drag an instance to the Stage. You may not need it, but if you do, it's there. And even if you don't end up referencing the instance name in ActionScript, it comes in handy as a reminder of which object you're tying your action to (see Figure 9-5).

    As you type, Flash occasionally "guesses" at what you want, popping up a list of options or filling in a bit of punctuation. You can either accept Flash's suggestions or just keep typing. Either way, when you finish, your Actions panel should look something like the one in Figure 9-5.

    Figure 9-5. Always double-check the Actions panel to make sure you're adding your action to the correct instance of the correct object. If you goof, your ActionScript code might be letter-perfectbut odds are, it won't work, and Flash won't be able to tell you why. (It's not a mind reader, after all.)

    Tip: Clicking the Actions panel's ScriptAssist icon (it looks like a magic wand) redisplays the Actions panel with the script pane at the bottom, and ActionScript hints at the top. Each time you click a line of ActionScript code in the script pane, Flash changes the hints to match. Figure 9-6 shows you an example of the Actions panel in ScriptAssist mode. (Click the ScriptAssist icon again to toggle the Actions panel back to normal mode.)
    Figure 9-6. Top: Clicking the ScriptAssist icon tells Flash to redisplay the Actions panel with hints at the top.
    Bottom: Some hints are more useful than others. You can stay in ScriptAssist mode if you like and continue to type your ActionScript code in the script pane. To return to normal mode, just click the ScriptAssist icon again.
  6. Test your action by choosing Control Test Movie .

    The Flash Player (test window) appears. As Figure 9-7 shows, clicking on Smiley's left eye lets you drag the face around the test window. When you let up on your mouse, Flash drops Smiley right where you repositioned him.




Flash 8
Flash Fox and Bono Bear (Chimps) (Chimps Series)
ISBN: 1901737438
EAN: 2147483647
Year: 2006
Pages: 126
Authors: Tessa Moore

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