One of the main obstacles for Flash designers and developers when switching to ActionScript 3 is the transition from scripting purely on the Timeline to writing and using custom AS3 classes. Although one can use Timeline AS3 scripting with Flash CS3 (in many of our tutorials we do just that), often it is more elegant and practical to place your code in external .as class files. This approach is more in line with the object-oriented nature of AS3. Also, there are many custom AS3 classes available (including on this site) and knowing how to incorporate them into your script can greatly enhance your applications.
There are three basic ways to include AS3 custom classes into your Flash CS3 application.
- Creating (or importing) a custom AS3 class and then instantiating the class in your script on the MainTimeline. You can then use the classes's methods and properties in your Timeline script.
- Attaching a custom AS3 class to a MovieClip that is created at authoring time. (This assignment is done via the Linkage item in the Library menu.)
- Using the Document Class new to Flash CS3 and AS3. When you use this approach, you do not place any code on the Timeline. All code resides in external files. ActionScript 3 purists consider this approach to be preferred. In fact, in most of AS3 books all examples are entirely class-based.
We find the first approach to be especially flexible and user-friendly. Thus, in this first tutorial we will illustrate the first approach. In the upcoming tutorials, we will discuss the other two approaches. In this tutorial, we introduce a simple applet. At first, all the code is on the MainTimeline. On page 3, we show how to transfer the code to a custom AS3 class. On subsequent pages, we show how to use the class to create enhanced versions of the applet via simple Timeline scripts that use the class.
- Download all files related to this tutorial in one zip file: as3_class_intro.zip
Why do we like the first approach? The first approach seems most suitable for the kind of applets that we as mathematicians ultimately want to create.
For example, in our 2D or 3D graphers:
A Function Grapher in ActionScript 3
A 3D Function Grapher in ActionScript 3
we have a large number of interrelated variables whose values depend on the user's input. It is convenient to place the code on the MainTimeline that keeps them all together. The Timeline code reads the user's input, uses the methods of the custom MathParser class to process it, then calls methods of another custom class, GraphingBoard (or GraphingBoard3D), to create the graph. (You will find links to the articles containing these graphers in Development Articles and Custom AS3 Classes section of this website.)
In this tutorial, we present a simple, generic non-mathematical applet.