Using Tools To Create Code

Using the Actions panel
You already added ActionScript to several of the projects you created in this book. It's necessary to add very simple scripts to move or stop the playhead on the Timeline. In the next chapters, you start to add much more ActionScript to your projects. Before you jump into using longer pieces of code and learning how to write ActionScript, you need to know where and how to edit the code.You can add ActionScript to your documents by typing code into the Actions panel (Window > Development Panels > Actions). The Actions panel lets you write and edit code that you place on the Timeline or directly on an instance, such as a movie clip, button, or component instance.The Actions panel has many invaluable tools for helping developers write robust ActionScript code (Figure 6.1). Some of the main tools include the following:Search and replace capabilitiesSyntax checkerError reportingCode formattingCode hintingCode completionSyntax coloringCode debuggerFind help
Figure 6.1. The Actions panel contains a few main sections: Actions toolbox, Script navigator, Script pane, and a toolbar containing several buttons.

Looking at ActionScript 1.0 and 2.0
It's likely that you've heard about ActionScript 2.0, so you might wonder how version 2.0 compares with versions in earlier versions of Flash. You will see code examples of both versions of ActionScript when you search online, so you should be aware of differences between the versions so you can choose what version of the language to use. ActionScript 2.0 is the latest version of ActionScript available in Flash MX 2004. However, it's not a requirement of Flash MX 2004 to use ActionScript 2.0you can use any version of ActionScript you want with the software. You can use ActionScript 2.0 for Flash Player 6 or Flash Player 7 files, however some features of ActionScript 2.0 only work with Flash Player 7.There are a few differences between the languages. For example, you can use strict data typing in ActionScript 2.0, but not ActionScript 1.0. This process improves the error-checking process, so it's a good practice to code using strict typing. You learn about strict data typing in Chapter 7 Another improvement in ActionScript 2.0 is improved support for writing object-oriented code. This helps build better applications by allowing you to intelligently organize code into classes, so it is mostly important for developers. You find out more about classes later in this chapter, but you won't use custom classes (classes you write yourself) in this book. You use only classes that are built into Flash.It's not necessary to use ActionScript 2.0's class-based coding, and it's sometimes overkill and just makes more work necessary in simple applications (or animation/graphics-based files). However, you will use some aspects of ActionScript 2.0 in this book. When you use any part of ActionScript 2.0, you need to publish your SWF files with ActionScript 2.0 selected as the version you are using. You need to select this setting in the Flash tab of the Publish Settings dialog box (File > Publish Settings) to correctly compile (publish) your SWF file.The syntax checking feature (using the Check Syntax button) runs through your ActionScript and checks that the code doesn't throw any errors when you publish it. You only need to click the Check Syntax button in the Actions panel to see whether your code has any syntax errors. Doing this can save you a lot of time because it lets you check that your syntax is valid before you publish the file. Otherwise, Flash might generate various error messages if there are problems with the ActionScript.One of the best features of Flash's ActionScript is its robust error reporting. You click the Check Syntax button in the Actions panel to check if your code has any syntax errors, and if any errors are found, Flash displays the Output panel. The Output panel displays information or a message about the code that contains errors, and it displays useful debugging information in the Output panel, such as the type of error, frame, and line numbers.Another great feature in the Actions panel is the code formatter (using the Auto Format button). When you click the Auto Format button, all the ActionScript in a Script pane is formatted (such as applying indenting and spacing) based on options you specify in your preferences. You can specify how the code formats by selecting Auto Format Options from the Actions panel options menu (Figure 6.2). The Auto Format Options dialog box opens, which allows you to modify five formatting options and also view how each option affects code by looking at a preview pane, as shown in the following figure.
Figure 6.2. Select Auto Format Options from the Actions panel options menu, which opens a dialog box in which you can select and preview formatting options.
[View full size image]

Using the Output Panel
Sometimes the Output panel opens and displays information about the ActionScript you have written or how the SWF file is working. The Output panel mainly does two things:It alerts you to any problems with your code.It returns values or display information.Information can be the result of actions in your code that you specifically write, so messages or values display in the panel. You can write these messages using a trace statement. You use the trace statement to send results or values to the Output panel, usually to troubleshoot problems or see what values return to the SWF. You learn much more about the trace statement later in this chapter.Code hinting and code completion help speed up development time, and they help you avoid typing mistakes. Code hints (Figure 6.3) can also help you learn ActionScript and teach you how to format code. Code hints help show you where brackets go, or what you can enter as parameters.
Figure 6.3. Code completion appears in the Script pane of the Actions pane and helps you finish typing your statements quickly.

Figure 6.4. Code hints, which appear in the Script pane, help you remember proper syntax.



Organizing code in an FLA file
There are three main ways to organize the ActionScript that you write for a project:On the Timeline:
Select a frame on the Timeline and type ActionScript into the Script pane of the Actions panel. A small "a" appears in the frame you added the script to (Figure 6.5).
Figure 6.5. A frame with code attached to it appears to have an a symbol in the frame.

Select an object on the Stage (such as a movie clip or a component) and then type ActionScript into the Script pane. The code you write is applied to that object, unless specified otherwise.Placed in external ActionScript files (or classes):
Type code into a code editor, such as the Script window in Flash MX Professional (or a text editor such as Notepad). The external code imports into the SWF file using a statement in the FLA file.
The preferred and officially (Macromedia) recommended way to write ActionScript is to place as much code as possible either in an external file or on the main Timeline. You should always avoid placing code directly onto instances because it quickly leads to a lot of duplicated code. It also makes the code snippets that you need to fix or edit difficult to find in an FLA file. Placing code all over a FLA file is sometimes referred to as "spaghetti code."Coding concepts."When you write code, it is best to group similar code together. For example, if you have large blocks of code that you use to create and modify similar items (such as movie clips) it makes sense to keep the code that does this together in one location. For example, if you have ActionScript that builds a menu system (or adds functionality to it), try and organize the code for the menu together in one place.

