The Basic Context for Programming in Flash


With the enhanced Actions panel (also known as the ActionScript editor) in Flash 8, you can program interactivity by writing interactive commands directly into the Script pane of the Actions panel.

New Feature 

Flash 8's Actions panel can now show you hidden characters, such as character spaces and line breaks, in your code. Choose Hidden Characters from the options menu of the Actions panel to see these characters. Due to the reactions of many Flash designers and beginner developers, the Actions panel also has a new Script Assist feature, which replaces the sorely missed "Normal mode" of Flash MX. For more information on Script Assist, see Chapter 18.

In the Actions panel, you can type your code from scratch, as well as insert your code with the help of Action booklets or the new Script Assist feature. Syntactically, ActionScript looks and feels very much like JavaScript. Macromedia has gone to great lengths to make ActionScript compatible with ECMAScript 4 (the standard programming guidelines derived from JavaScript). And like other object-oriented languages, ActionScript is composed of many familiar building blocks: variables, operators, conditionals, loops, expressions, built-in properties, subroutines, and native functions.

Accessing ActionScript Commands

All of the ActionScript commands are found in the Flash interface in the Action booklets or plus (+) button menu of the Actions panel. However, assembling actions with one another is not something Flash 8 automatically performs unless you use the Behaviors panel to add interactive functionality to your movie elements. Although it is beyond the scope of this chapter to fully explain fundamental programming principles, we can give you a sense of the whole of ActionScript by providing an organized reference to each of its parts.

Actions List Organization in the Actions Panel

In the Actions panel, you can cut, copy, and paste code within the Script pane from one area of your movie to another using Ctrl+X (z+X), Ctrl+C (z+C), and Ctrl+V (z+V), respectively. You are free to select partial or entire lines of code, and modify the code in any way you want. With Flash 8, you can even edit your code in your preferred text editor! If you want to create your own programming macros in other programming applications, you can write your scripts outside of the Flash authoring environment and copy the final code into the Actions panel when you're done.

Tip 

To make sure that you don't have any syntax errors after reorganizing code, click the Check Syntax button in the Actions panel toolbar. Flash alerts you if there are scripting errors by placing messages in the Output panel.

Also, use the new Script Navigator in the lower-left corner of the Actions panel to quickly switch back and forth between keyframe or object code. For example, you can choose one frame in the Script Navigator, copy its code, and then switch to another frame to paste the code.

If you decide to write ActionScript with another editor, you can use the #include directive to load the contents of the external code file (.as) into the Flash movie at publish or export time. For more information on the #include directive, read Chapter 32, "Managing and Troubleshooting Flash Movies".

image from book
Using Esc Shortcut Keys for Actions

In Flash 8, you can choose to show or hide shortcut keys in the Actions panel. In the options menu of the Actions panel, choose Esc Shortcut Keys (if it's not already checked). Now, click the plus (+) menu of the Actions panel to access ActionScript commands. You'll notice that shortcut keys are defined after the name of the command. For example, loadMovie(), in the Global Functions ð Browser/Network menu, has a keyboard shortcut of Esc+L+M. If you give the Actions panel focus and press the Esc key, then the L key, then the M key, the loadMovie action appears in the actions list of the Script pane, complete with placeholders for arguments. The shortcut will not work if you try to press the keys simultaneously — you must press the keys in sequence, as described in the previous example. Each key must be pressed and released before you type the following key(s) in the shortcut.

image from book

The Help Panel

Don't know what the methods of the Sound object are? Need to see if the getVersion() function will work in a Flash Player 4-compatible movie? Flash 8 has conveniently nested everything related to help documentation in the Help panel. Among other items, the new Help panel contains all the syntax of the ActionScript language. You can access the Help panel in a few ways:

  • Choose Help ð Flash Help, or press F1.

  • Click the Reference icon in the Actions panel.

  • Right-click (or Control+click on Mac) an action in the left-hand Script pane of the Actions panel and choose View Help in the contextual menu.

We'll quickly show you how to use the new Help panel in an actual Flash document:

  1. Open a new document (File ð New).

  2. Select frame 1 of Layer 1, and open the Actions panel by pressing F9.

  3. Open the Global Functions booklet and click the Browser/Network booklet. Double-click the loadMovie action. The action will appear in the Script pane, as shown in Figure 24-1.

  4. Select the text loadMovie in the Script pane, making sure not to select the (); portion of the action. Click the Help icon (that is, the circle with the question mark) at the top right of the Actions panel. The Help panel will open, displaying the definition for the loadMovie() action, as shown in Figure 24-2.

image from book
Figure 24-1: The Actions panel with a loadMovie() action in the Script pane

image from book
Figure 24-2: The Help panel with the definition for loadMovie

You can select other actions from the left pane of the Help panel and view their descriptions.

Tip 

You can also print descriptions from the Help panel by choosing Print from the options menu at the top-right corner of the panel, or clicking the Printer icon in the Help panel toolbar.

ActionScript 1.0 and 2.0

So why all the fuss about ActionScript now? Let's make one thing clear right away: For most Flash designers and developers, or rather for most of the work that's done in Flash, the graduation of ActionScript 1.0 to 2.0 may mean next to nothing at all. This isn't to say that ActionScript 2.0 should be ignored or is not a monumental leap forward for Flash programming — we simply want to calm or allay any fears you might have after reading about ActionScript 2.0 from other sources. If you've used ActionScript in Flash versions prior to MX 2004, then ActionScript 1.0 refers to the coding styles that those versions of the application used.

image from book
Using the #include Action

ActionScript has a processing directive that enables you to insert external text files (with an .as file extension). You can write ActionScript in any text or script editor and save that text separately from the Flash document file (.fla). When you publish a Flash movie file (.swf) from the Flash document, Flash 8 will retrieve the .as file and insert the actions to the action list where the #include action was issued. For example, the following code can be written in a contact.as file, which, as the name implies, contains a person's contact information for the Flash movie:

 var contactName:String = "Joseph Farnsworth"; var contactStreet:String = "675 Locust Street"; var contactCity:String = "Chicago"; var contactState:String = "IL"; var contactPhone:String = "312-555-1342"; var contactEmail:String = "jfarnsworth@mycompany.com"; 

Note that this code is written in the ActionScript 2.0 format, which we discuss in this chapter. In a Flash document, you could insert this code into a keyframe of the Main Timeline (or a Movie Clip timeline) by using the #include action. You can use the #include action within any Flash event handler including keyframe, Button instance, onClipEvent, and so on:

 #include "contact.as" 

Make sure you do not insert a semicolon at the end of the #include line. Think of the #include action as a special tag for Flash 8, letting it know that it should replace the #include line of code with all the code within the referred file. The following code will result in a "malformed" error in the Output window, upon testing or publishing the Flash document:

 #include "contact.as"; 

Why is the #include action useful? For experienced programmers, the #include command gives you the freedom to write ActionScript in any text editor. You can define entire code libraries of custom functions. You can then reuse the .as files from movie to movie. In ActionScript 2.0, custom classes must be defined in a separate .as file, but they do not use the #include directive; rather, AS2 classes use the import keyword.

Note that the #include action is executed only upon publishing or testing the Flash movie. You cannot upload .as files to your Web server for "live" insertion of Flash ActionScript. Anytime you change the .as file, you will need to republish your Flash movie file (.swf).

You can find more information on the #include action in Chapter 32, "Managing and Troubleshooting Flash Movies." As we discuss in Chapter 3, "Planning Flash Projects," you can track all of the .as files in the Project panel of Flash Professional 8 as well.

image from book

ActionScript 2.0 introduces a couple of major structural changes to the way in which you can code Flash ActionScript. To begin with, it uses strong data typing. You'll learn more about data typing in Chapter 26, "Using Functions and Arrays," but for now, know that ActionScript 2.0 imposes some rules on how you can assign values to variables, functions, and other objects in your code. Unless you're creating code that uses custom classes and object-oriented programming concepts such as inheritance and prototypes, you won't really need to worry about whether your code will work as ActionScript 2.0 code. Another big change with ActionScript 2.0 is case-sensitivity. Prior to Flash Player 7, most ActionScript terms were case-insensitive, meaning you could refer to object references or variables with varying cases, such as:

 var firstName = "Robert"; var lastName = "Reinhardt"; var fullName = firstName + " "+ lastName; 

In ActionScript 1.0, you could have used the following code as the last line, without receiving an error:

 var fullName = firstname + " "+ lastname; 

However, in ActionScript 2.0, that same line of code would be incorrect because firstname and lastname are not using the same case as the original variables.

Why should the distinction between ActionScript 1.0 and 2.0 even be an issue? For starters, when you publish a Flash movie for Flash Player 6, 7, or 8, you need to decide how Flash 8 will compile the ActionScript code in your document. In the Publish Settings dialog box, you can choose which ActionScript version to publish in the Flash tab. Unless directed otherwise, you'll publish most of the Flash movies for Part VII of this book as ActionScript 2.0, even though it pretty much looks and feels like ActionScript 1.0.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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