Adding Frame Actions


Some of the most basic scripting tasks involve controlling movie playback: making your movie stop and start and jump from place to place. By default, a movie begins running at playback; you can change that in the Publish Settings (see Chapter 16), but you can also do it more definitively with actions.

To make your first script in Flash, set up a multiframe document that has identifying text in each frame. Then add a stop action to keyframe 1, to make the movie start out paused at playback. As you create other scripts to navigate the movie, you can easily see their results. Save this file as a template for use in other scripting tasks.

To set up a document for testing frame actions

1.

Create a Flash document with three layers: Actions, Scene, and Frames.

2.

In the Scene layer, add in-between frames in frames 25; there is a keyframe in the first frame by default.

3.

On the Stage, for keyframe 1, create text that identifies the scene (Scene 1).

4.

In the Frames layer, create keyframes in frames 15.

5.

On the Stage, for each keyframe, add text that identifies the frame number (Frame 1, Frame 2, and so on).

Your document should look like Figure 13.6.

Figure 13.6. To test frame actionsactions that instruct Flash to move to a specific frame or to start and stop playbackit's useful to have a document that identifies each frame. That way, you can see the results of your scripts easily.


The Pitfall of Placing Actions on Multiple Layers

At each point in the Timeline, Flash implements the actions in the highest-level keyframe that contains an action. Imagine a three-layer movie. Frame 2 of the top layer contains no actions. In frame 2 of the middle layer, a keyframe contains an action telling Flash to skip to frame 5. In frame 2 of the bottom layer, a keyframe contains an action telling Flash to skip to frame 10. When you play this movie, and the playhead hits frame 2, Flash looks in the top layer for actions and finds none, moves to the next layer down, finds an instruction and follows it, and so on, creating a queue of instructions. If you reorder layers Flash carries out the actions in the new order. This situation can cause havoc with your movie.


To begin scripting by adding comments

1.

Continuing with the document you created in the preceding task, in the Actions layer, select keyframe 1.

2.

Access the Actions panel.

The name Actions-Frame appears in the title bar of the Actions panel. If the panel isn't open, choose Window > Actions.

3.

From the Options menu, in the upper right corner of the Actions-Frame panel, make sure Line Numbers is active (Figure 13.7).

Figure 13.7. Open the Options menu in the Actions panel to make sure that line numbering and word wrap are active (if not, select them to activate them). Visible line numbers make it easier to keep your place while scripting; word wrap makes it easier to view scripts as it forces them to stay within the visible Script pane area.


Visible line numbers make it easier to keep your place as you script. (Line Numbers is turned on by default; a check mark appears next to the name in the menu when it's active.)

4.

From the Options menu, choose Word Wrap.

Word Wrap forces the lines of your script to break to fit within the Script pane. Note that such line breaks aren't meaningful in the script itself; the ActionScript syntax tells Flash where the meaningful divisions in script text occur (see the sidebar "The Mystery of ActionScript Syntax").

5.

In the toolbar of the Actions-Frame panel, check that Script Assist is active. If necessary, click the Script Assist button to open the Script Assist window (Figure 13.8).

Figure 13.8. Clicking the Script Assist button in the toolbar of the Actions panel opens a pane where information and settings appear to help you with scripting.


6.

In the Actions Toolbox, click the Global Functions category.

A list of subcategories appears in the Actions Toolbox; a description of the Global Functions category appears in the Script Assist window.

7.

Click the Miscellaneous Functions subcategory.

A list of actions appears in the Actions Toolbox.

8.

In the list of miscellaneous functions, click Comment.

A description of the comment action appears in the Script Assist window (Figure 13.9).

Figure 13.9. To view categories and subcategories in the Actions Toolbox, single-click the name or the book icon to its left; the item expands to reveal its contents. As you select items in the Actions Toolbox, Script Assist places the appropriate information and options in the Script Assist window.


About Comment Lines

The double slash is known as a comment delimiter; the delimiter sets the boundaries of a comment within a script. Flash ignores any text between the two slashes and the next paragraph return for the purposes of scripting. When compiling the script for playback, Flash leaves that text out of the final file, so you don't need to worry that your comments will increase the .swf file size. It's a good idea to write notes about your script to remind yourself what you intend the script to do. Comments will also help anyone who needs to modify your script later.

For long comments, there's what's called a multiline delimiter. (Choose the Multiline check box in the Script Assist window to add a multiline comment to your script.) A multiline comment begins with /* and ends with */. Flash excludes everything in between the opening and closing delimiters from the script.

When you're testing long scripts or trying different ways to achieve your scripting task, it can be useful to temporarily remove part of the script to see what happens. Comment delimiters let you do this quickly without deleting any code. Script Assist won't help you with this, but you can turn off Script Assist and then manually insert double slashes at the front of a line or two of the script. If you want to block out a large section of the script temporarily, use the /* and */ delimiters.


9.

To add the comment action to the script, do either of the following,

  • Double-click the comment action in the Actions Toolbox.

  • Drag the comment action from the Actions Toolbox to the Script pane.

Instructions and a text field for entering comment text appear in the Script Assist window. Flash adds two slashes (//) to line 1 of the Script pane and selects them.

10.

Click the Comment field in the Script Assist window to activate the field.

11.

Type Pause the movie on frame 1 at runtime (Figure 13.10).

Figure 13.10. The two slashes indicate the beginning of a comment. When you add a comment action using Script Assist, Flash adds slashes to the script automatically. The text you enter in the Comment field is added to the script after the slashes.


Flash adds your comments to the Script pane. With word wrap turned on, Flash wraps your text to fit in the Script Pane; in the real script, the comment is just one line.

Tip

  • Comments are so easy to enter that you may prefer to skip using Script Assist. With Script Assist inactive, click in the Script pane, type your comment delimiter, and then type your comment text. Turn on Script Assist when you're done to get help with the rest of your script.


To set the movie to pause at playback

1.

Continuing with the document you created in the preceding task, in the Actions Toolbox, click the Global Functions category to expand it, and then click the Timeline Control subcategory.

A list of actions for controlling the playback of the movie appears.

2.

To add an action that pauses the Timeline, double-click the stop action (or drag it to the Script pane).

Flash adds the new action below the comment line (Figure 13.11).

Figure 13.11. With the comment code selected in Script Assist mode, Flash adds the next action you double-click (here it's stop) below the existing comment text.


3.

Save the document as a template for future use; name it FrameActionsTemplate.

(For detailed instructions about saving documents as templates, see Chapter 1.)

4.

Close the document.

You don't want to inadvertently make more changes to your master template document, so close it at this point.

Tip

  • Instead of using the Actions Toolbox to find and add actions, you can use the Add menu in the Actions panel. Click the plus sign located above the line numbers in the Script pane to view the menu. Choose Category > Subcategory > action (for example, Global Functions > Timeline Control > stop); Flash adds the selected action to your script.


The Mystery of ActionScript Syntax

ActionScript has its own rules, which are analogous to the rules of grammar and spelling in English. These rules, called syntax, govern such things as word order, capitalization, and punctuation of action statements. When you use Script Assist or the Behaviors panel, Flash handles all the details of syntax for you. When you start moving toward more independent scripting, you need to handle these details yourself. The following list briefly describes four crucial ActionScript punctuation marks that you'll see in those scripts.

Dot (.). ActionScript uses dot syntax, meaning that periods act as links between objects and the properties (characteristics) and methods (behaviors) applied to them. In the statement the dot (the period) links the object (a movie clip named clone_mc) with the method (duplicatMovieClip) that creates a copy of the object.

clone_mc.duplicateMovieClip


ActionScript also uses the dot to indicate the hierarchy of files and folders in path names for targeting objects (such as movie-clip symbol instances) within one Flash file, similar to the way HTML syntax uses a slash. (Note that Flash 3 and 4 used slashes to indicate these types of path names. Flash 8 and Player 8 still recognize this slash syntax, but ActionScript 2.0 doesn't.) Macromedia recommends using dot syntax unless you're creating files for playback in Flash 3 or 4, and that's what you'll find in the scripts created by Script Assist and the Behaviors panel.

Semicolon (;). A semicolon indicates the end of a statement. The semicolon isn't requiredFlash interprets the end of the line of statements correctly without itbut including it is good scripting practice. The semicolon also acts as a separator in some action statements.

Braces ({}). Braces set off ActionScript statements that belong together. A set of actions that are supposed to take place after on (release), for example, must be set off by braces.

Note that the action statements within braces can require their own beginning and ending braces. The opening and closing braces must pair up evenly. When you use Script Assist or the Behaviors panel to enter multiple actions in a script, Flash handles positioning of braces, ensuring that the pairs match. When you enter multiple actions without assistance, you must pay close attention to where you're adding statements and braces within the Script pane to ensure that you group the actions as you intend.

Parentheses (()). Parentheses group the arguments that apply to a particular statementdefining the scene and frame in a goto action, for example. Parentheses also allow you to group operations, such as mathematical calculations, so that they take place in the right order.





Macromedia Flash 8 for Windows & Macintosh Visual QuickStart Guide
Macromedia Flash 8 for Windows & Macintosh
ISBN: 0321349636
EAN: 2147483647
Year: 2005
Pages: 204

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