AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

Actionscript 2 Classes


As mentioned previously, every actionscript class contains its own defined set of properties (attributes of the class, like the MovieClip class's _x and _currentframe properties) and methods (functions, like the MovieClip class's gotoAndStop method). In addition to the built-in classes like Math, Date and Sound, which we'll look at in this section, you can also write your own classes. Because I use some of these in the following pages, I thought a little overview here would be in order.

First, a note about the built-in classes: in Flash MX 2004 all predefined classes are saved in a directory set up when Flash is installed. On my PC, that directory is Program Files\Macromedia\Flash MX 2004\en\First Run\Classes. The built-in classes in Flash 8 are saved in a directory with the same path, except Flash 8 is substituted for Flash MX 2004.

Now, about your own classes (or others you download): in ActionScript 2 (the version of ActionScript introduced in Flash MX 2004), you can define your own class by putting the code which defines the class in a separate .as (actionscript) file with the name of your class. A file named ScreenManager.as would contain code to define the ScreenManager class, for example.

Where to keep (non-built-in) class files

Class files can either be saved in the same directory as the fla which uses them, or in a directory specifically set up to hold class files (especially useful if you use class files from multiple sources). If you create a separate directory to hold all of your own defined classes, you can tell Flash where to find it by choosing Edit, Preferences, selecting the Actionscript tab, clicking Actionscript 2.0 Settings, and adding the directory path to the classpath list. On my PC, for example, user-defined classes are stored in c:/AS2Classes, and I have the classpath information set up to look for a class in the default Flash class directory first, then the current directory (where the fla that's using the class is), and then my AS2Classes directory, as shown:

Then in the AS2Classes directory, I have folders set up for every domain I have downloaded classes from (eg, AS2Classes/org/dembicki, for classes from www.dembicki.org) or for which I have defined classes (eg, AS2Classes/com/flashcreations, for classes I use at this site). This insures that each class can be uniquely identified even if I get two classes of the same name from different sources. Each of those folders may contain further subdividing folders for classes providing different types of functionality. For example, I have a utils folder under com/flashcreations, in which I put classes that extend the builtin Math and String classes.

In the following pages, I'll give some examples of using the built-in Flash classes and introduce a few extra classes that provide extensions to Flash's built-in functionality.

What does a class file look like?

Generally speaking, a class file contains

You'll see examples of class files in the pages of this section, so I won't go into more detail here.

How to use class files

In order to include any classes that are not built into Flash in your movies, you need to put an import statement in the class file or frame that is using the class. Eg, if the class file MathExtra.as is in the same directory as the fla that uses it:

import MathExtra;
or, if you have class files saved in a central class directory, with subdirectories for different source domains:
import com.flashcreations.utils.MathExtra;
Then all the properties and methods of the class become available for your program to use.

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads