Creating a Document Class

Creating a Document Class

Before writing any code, you need a Flash document to which you can attach your code.

Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques

1. Create a new ActionScript 3.0 document by choosing File > New and selecting ActionScript 3.0 (Figure 3.4).

Finished versions of all the classes and examples in this chapter are available in the Chapter 3 folder on the CD included with this book.

Figure 3.4 The New Document dialog box allows you to choose what type of document to create.

2. For your document to locate your class, you’ll want to make sure that both your Flash file and your Action-

Script file are saved. Choose File > Save As to save your document, create and navigate to a new folder called examples, name the document DocumentExample.fla, and click OK.

3. Now you’ll create the class file. Choose File > New and select ActionScript 3.0 Class. This generates a

prompt asking you to name your class. Name the class As a matter of convention, class

names should begin with a capital DocumentExample (Figure 3.5). Click OK.

letter. This opens the ActionScript in the Flash Code Editor.

Figure 3.5 The prompt to name your new class. Once you provide a name, Flash sets up the basic structure of the class for you.

Chapter 3 Introduction to ActionScript Classes

4. Choose File > Save As and save the file as DocumentExample.as in the examples directory that

you created in step 2.

5. Return to DocumentExample.fla, and attach the Be sure to save your class before class to the document by typing the class name

you start adding any code. Many of DocumentExample into the Class field in the

the bells and whistles added to the Properties panel (Figure 3.6). Code Editor in CS5 depend on your file being saved first.

Figure 3.6 The Class field in the Properties panel allows you to assign a document class.

Basic Class Structure

6. Make sure that Control > Test Movie > in Flash Profes- After you’ve provided a name for your class, Flash sional (rather than in Device Central) is chosen and

sets up the necessary structure of the class file for press Command+Return/Ctrl+Enter to test the movie.

you (Figure 3.7). This structure includes the pack- age block (starting with the keyword package You’ll receive the following error in the Compiler ), the class block (starting with public class ), Errors panel:

and the constructor method (starting with public function and a function name that

5000: The class 'DocumentExample' must subclass matches your class name). ➥ 'flash.display.MovieClip' since it is linked to a

➥ library symbol of that type. Every class must be in a package. The package

This error occurs because the Flash document relies gives the class context (you’ll do more with pack- on the methods and properties found in the MovieClip

ages later in this chapter). The class block defines class. Although you can write all kinds of new methods the start and end of the class code. The constructor method is called when an instance of the class

and properties for your document class, you need to is created. Any code within the constructor will start by extending the MovieClip class using the extends

run when the method is called. The name of the keyword.

constructor method must match the class name exactly .

7. Add the following highlighted code to the class statement:

class DocumentExample extends flash.display. ➥ MovieClip {

As you type, the Code Editor will display a list of packages or classes available. You can navigate the list of choices with your keyboard or mouse. Press the

Figure 3.7 The basic class structure created

Return/Enter key when the correct item is selected in

automatically by Flash.

Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques

the list to save yourself some typing. The Code Editor will automatically add an import statement above the line you just added (you’ll learn about import state- ments in a moment).

8. Save your class and test your movie again (press Command+Return/Ctrl+Enter). You can test your

movie from the Code Editor if you like. You should now Be sure to save frequently. Always

have 0 errors listed in your Compiler panel. Although save your document and/or

you don’t have any errors, you also don’t yet have much ActionScript files before testing

confirmation that your class has been instantiated (your your movie.

movie is empty).

9. Replace the comment inside the constructor method block ( // constructor code ) with this trace statement:

trace("hello");

10. Save your class and test your movie again. You should now see the word “hello” in the Output panel

(Figure 3.8), proving that your class has successfully been instantiated.

Figure 3.8 The Output panel shows any mes- sages passed using the trace method.

The methods and properties for document classes vary depending on the nature of each specific project. In gen- eral, the document class is not one that will be reused from project to project. You’ll find that a document class is not necessary for every animation project.

To determine whether you require a document class, think about what information each object requires in your project. Place all your objects on a need-to-know basis (as if they were government spies under your supervision). If the objects within your project can act independently without your document acting as supervisor, by all means omit the document class. Conversely, if you find that there is infor- mation that must be present at the document level, use a

Chapter 3 Introduction to ActionScript Classes

document class (for example, the web project in Chapter 5 utilizes a document class). Sometimes a document class will

be the simplest way to implement the desired functionality. Most of the examples later in this chapter focus on a dif- ferent kind of class, but keep the document class in mind when planning your Flash projects.

To reference any other classes within your ActionScript file, as you did when DocumentExample extended the MovieClip class, you need a way to point to other classes. Enter classpaths.