How to use a class

Starting with Actionscript 2, Flash allows you to write Classes. Classes are pieces of reusable code, used to easily add new functionality to Actionscript code, to make things easier and faster. Using a class is a bit like including a file from the old days of Actionscript, with #include, but there are several big differences in practice.

Tweener itself is also a class. More than that, it uses several different classes together, for some specific tasks. This kind of specialization - and the separation of scope - is what make classes the main principle behind Object Oriented Programming, or OOP.

Much like #include files, you install classes by copying them to a directory and using them. So the first thing you must do when you're installing a class is to decide where you want to put it: a specific project folder, or a global folder.

Using the class globally

If you're installing a global folder - that is, a global classpath - all of your projects will have access to this same class, so you won't have to copy anything to specific project folders. You'll also be able to update the class globally. For example, on Flash CS3 on a PC, the global class path is usually C:\Program Files\Adobe\Adobe Flash CS3\en\First Run\Classes. Other environments will have similar paths that can be used or will let you specify the global class path.

From a generic point of view, using global classpaths poses two disadvantages:

Because of this, it's usually recommended to have classes locally.

Using classes locally

You can also have classes on the folder of your project, meaning the same folder as your .FLA file when using the Flash IDE (other environments will have a project folder of its own). As per the default configuration, Flash automatically searches that folder for classes, so everything there will automatically be included on your movie as long as you imported it as classes.

The advantage with approach is to have class files that are specific to your project. Source packaging and redistribution is easier, and you're safe from unwanted updates.

This is the recommended approach when installing Tweener classes.

Using Tweener classes

So, in Tweener's case, after installing the Tweener classes (by unzipping the download .zip file on your .FLA's folder, or copying the files over), you would probably have a list of files as such:


Where "myWebsiteProject.fla" is your hypothetical .FLA file, considering you're working with the Flash IDE.

The way folders are structured matter for a class. For example, on Tweener's complete class name is caurina.transitions.Tweener; that's why the file (and all other class files) have to be on folder /caurina/transitions.

The caurina name is meant to be something unique, neutral; from a technical standpoint, it doesn't matter much, so it's just part of Tweener's package path.

If (and only if) you're using some unusual folder for your class files - something other than the project folder, for example - you have to make sure the correct class directory is listed on the document's classpath - the list of folders Flash will look for when it tries to add a class to the project when compiling. On the Flash IDE, you can access the document's classpath list by using the "File" > "Publish Settings" menu, then clicking on the "Settings..." button next to the Actionscript version combo box. On the window that opens, make sure the class directory you've chosen is listed.

You can also change your global classpath settings on Flash preferences.

After you have a class installed, and it is acessible from your project, you can use it when writing Actionscript code. You can use classes by just referring to them, using their complete class path and name. There's no need to refer to the specific folder where your classes are copied (other than the complete class name with classpath). For example, to call a Tweener method, you would do this:

caurina.transitions.Tweener.addTween(mymc, {_alpha:100, time:1});

This is a bit too long, however. Fortunatelly, Flash lets you import classes and reuse them. Like this:

import caurina.transitions.Tweener;

Tweener.addTween(mymc, {_alpha:100, time:1});

On the surface, this works a bit like #include, but it's more of a declaration that you're gonna use the Class than anything. It doesn't actually include the contents of the class file every time you do an import. Instead, it includes the used class files on the first frame of your movie (or whichever frame you have chosen to be the class frame).

Also, remember that, if you're writing your code on the timeline (as opposed to in a new class), you have to repeat the import line on every new frame or MovieClip scope you intend to call Tweener.

See also