Why use document class?In Actionscript 3, we suggest you remove all codes from the Timeline and specific instance. Instead of that, we should write our codes in a document class. With a document class, you get to write pure object-oriented code in your class, which can work with all the other classes within the project scope, as well as exported items in your library or named instances sitting on the stage in your .fla. What is document class?A Document class is a top level class or top level object that is created when your Flash movie is loaded into the Flash Player. Writing a document class?Click File->New and choosing a new ‘ActionScript File.’ Let’s call the file “MainClass.as”. The sample code of a document class is like that:
package {
import flash.display.MovieClip;
public class MainClass extends MovieClip {
public function MainClass() {
trace ('Your first document class.');
}
}
}
If you already create a .fla file, just put the file "MainClass.as" to the same directory as your .fla file. Now we will declare our Document class from the Properties Inspector. This is our new way of associating our ActionScript with our Flash movie.
With the Selection tool, click on the stage to display the Document properties in the Properties Inspector. At the bottom of the panel, you will see a text input box where you can enter the name of your new Document class. In this dialog box, enter the name of our external ActionScript file without the .as extension. If you remember, we called this file MainClass.as in the previous tutorial. So you will enter "MainClass" in the box. |
Tutorial
|