Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Overview of ActionScript 2.0

Overview of ActionScript 2.0

ActionScript has been rewritten from the ground up based on the ECMAScript 4 specification. Most noticeably, ActionScript 2.0 has a new syntax and many new features. The new syntax includes several keywords that add case-sensitivity, strong variable typing, classes, variable scoping, inheritance, interfaces, and packages. At first glance, the language looks odd. Fortunately, it takes little time to learn.

Classes

If you have written ActionScript for Flash MX or Flash 5, you may have encountered the concept of classes. If you haven’t, you can think of classes as code blocks containing instructions for building objects with predefined methods and properties. Classes are useful when you need to define a class of objects that all have a similar behavior and interface. In ActionScript 1.0, you could create classes by writing constructor functions that, when invoked using the new keyword, created objects with methods and properties defined in the constructor. Since ActionScript 1.0 was a prototype-based language, you could add new methods and properties to the constructor function using the prototype keyword. Though classes in ActionScript were useful for many applications, their limitations made them cumbersome to work with in certain situations.

ActionScript 2.0 takes a very different approach to classes than its previous incarnation. Classes are now more akin to the classes in more robust programming languages such as Java, making Flash better suited for the web application development arena into which it has stepped.

However, this does not mean that the way you develop in Flash must change drastically. In fact, creating classes in ActionScript is relatively straightforward. For example, the code block below creates an Automobile class that has the color, numberofdoors, and engine properties and the turn(), driveForward(), and stop() methods:

class Automobile {
public var color : Number;
public var numberofdoors : Number;
private var engine: String;
function Automobile () {
engine = "Gasoline";
numberofdoors = 2; color = 0x000000;
}
public function drive() : Void {
// method for driving forward
}
public function stop() : Void {
// method for stopping
}
public function turn (angle:Number) : Void {
// method for recharging battery
}
}

Notice that the properties and methods within the class are followed by a variable type and preceded by a public or private keyword. Public properties can be called and modified by any code outside an instance of the class. Private properties and methods may only be read and manipulated by the object instance itself. This ability to scope properties and methods has important repercussions that make programming in a multideveloper environment much less prone to variable naming collisions.

Extending Classes and Inheritance

ActionScript 2.0 supports subclassing through a mechanism called extension, an easy way to take a generic class definition and refine it to create a more specialized class. Going back to the hypothetical Automobile class discussed earlier, one could take and extend the Automobile class into a new HybridElectric class, as follows:

	class HybridElectric extends Automobile{
function HybridElectric () {
engine = "Hybrid";
}
public function drive() : Void {
// method for driving forward using less fuel
}
public function stop() : Void {
// method for stopping that returns energy to battery.
}
public function recharge () : Void {
// method for recharging battery
}
}

This class definition contains new definitions for its stop() and drive() parent class methods. These methods are redefined in the new class in order to override the superclass’s definition with customized behavior. The new recharge() method defines specialized behavior for the new subclass, while the color and numberofdoors properties, in addition to the turn() method, are all inherited from the Automobile superclass. The ability for subclasses to inherit from superclasses makes it easy to create new classes with special properties without having to reimplement all of the class’s methods and properties for each subclass.

Interfaces

Interfaces are a completely new concept in ActionScript. They allow you to declare a class’s methods without defining the implementation. Interfaces also act as variable types, meaning that functions can take interfaces as arguments or return interfaces as the result. Because interfaces do not specify the implementation within a given class, the inner workings of the methods declared by an interface can be unique to the class that implements the interface.

Using the interface keyword, you can create a new interface definition, called Driveable:

interface Driveable {
function drive () : Void;
function stop () : Void;
function turn (direction:Number) : Void;
}

Now rewrite the Automobile class to implement the interface:

class Automobile implements Driveable{
public var color : Number;
public var numberofdoors : Number;
private var engine: String;
function Automobile () {
engine = "Gasoline";
numberofdoors = 4;
color = 0x000000;

}
public function drive() : Void {
// method for driving forward
}
public function stop() : Void {

// method for stopping
}
public function turn (angle:Number) : Void {
// method for recharging battery

}
}

The real power of an interface is in its ability to act as variable type. If a class implements an interface, it can be passed to any method that takes an argument of the interface’s type, regardless of the class’s type. For example, if you wrote a motorcycle or bicycle class that implemented the Driveable interface, you could substitute any one of these types of transportation for another because they all have drive(), stop() and turn() methods.

Unlike extending classes, a class may implement any number of interfaces as long as the class includes a definition for the methods declared in each interface.

Packages and Import

You must place each class and interface definition in ActionScript 2.0 in a separate ActionScript file on your computer’s file system. Often developers choose to organize their files into folders in a way that mirrors the relationship between the classes, called packages. When you place classes into packages, you have to change their name to reflect their new location. For example, if you chose to place the Automobile class and the Drivable interface in the classes and interfaces folders, you’d have to change the names to the following:

class classes.Automobile { … }

interface interfaces.Driveable { … }

When the name of the class or interface reflects its package, it’s considered to be a fully qualified name.

The modified Automobile class included an import directive at the beginning of the class definition. This line tells the compiler to import the Driveable interface from the file of the same name. Because the file is in different directories and is associated with packages, you need to change the import line to reflect the fully qualified name:

import interfaces.Driveable;

This may seem like a lot of extra effort, but the advantage to organizing the files this way saves you time when you include entire packages in a project. For example, if the interfaces directory contained multiple interfaces that were implemented in the Automobile class, then you could import all the interfaces with one line:

import interfaces.*;

Comments