Accessibility

Flash Article

 

Exploring the version 2 component architecture in Flash 8


Table of Contents

Comments

Examining the ActionScript 2.0 Language

Version 2 of the Flash 8 component architecture is written entirely in ActionScript 2.0, an enhanced version of the ActionScript language derived from the ECMAScript Edition 4 Netscape Proposal and JavaScript 2.0 Proposal. ActionScript 2.0 allows you to use higher-level language constructs for class and interface definitions; stronger typing of variables; parameters and return values; static, public, and private class members; and other enhancements geared towards a more familiar model for better and faster coding.

Classes, interfaces, packages, and attributes are among some of the long-awaited conceptual enhancements to ActionScript.

Classes and Interfaces

The class and interface keywords abstract much of the low-level details of object inheritance and prototypes. While creating a class in Macromedia Flash MX required modifying its prototype and strange syntax like Object.registerClass(), now the syntax becomes much clearer and more well-known:

class MyClass extends MovieClip {
...
}
          

You now declare members inside the body of the class definition.

You declare interfaces the same way. An interface is a set of methods defined by name, arguments, and return values only—no implementations. An interface is useful for creating a set of classes that have the same methods, while leveraging the compiler's error-checking capabilities. A class implements an interface using the implements keyword:

class MyClass implements Serializable {
...
}
          

Interfaces and class definitions exist only in external ActionScript files and are optionally linked to movie clip symbols in the library of a FLA. Once that linking occurs (using the Linkage options of a movie clip symbol), instances of the symbol are associated with the class and all methods and properties defined by that class are available on the instance. Additionally, the component's properties appear in the Component Parameters panel for easy editing inside Flash.

Find more information about ActionScript 2.0 in the Using Flash guide.

Packages

One of the most time-saving enhancements in ActionScript 2.0 is the package, a means for managing classes in large projects. Packages allow you to group classes into namespaces for both conceptual management and physical management on disk. A package consists of an ActionScript file that defines a class whose name consists of a path plus the class name. The path corresponds to the location of the class file on disk, starting at the root of the class path. The compiler imports any classes you reference in your code, provided it knows where to find the class.

This is where packages come in handy. For example, the ActionScript file defining the Label class exists in mx/controls/Label. Accordingly, you declare the class as follows:

class mx.controls.Label extends UIObject {
...
}
          

In this case, both "mx" and "controls" are namespaces. They exist in the default class path for Flash at the same location as the v2 architecture source code mentioned previously.

You can find all Flash 8 components here. Looking in this folder, you will find the "mx" directory and, nested inside it, "controls." Inside "mx/controls" is the ActionScript file, Label.as, which defines the class.

Compile the Label class along with your project using this line of code:

import mx.controls.Label;

This won't make a Label appear on the Stage or in the SWF because you didn't include any Label graphic elements in the library. To do so, drag the Label from the Components panel onto the Stage.

Find more information about packages and how to use them in the Using Flash guide.

Attributes

Use attributes to specify metadata that the authoring tool uses to do the following:

  • Populate the Properties tab of the Component Parameters panel
  • Populate the Schema tab of the Component Parameters panel
  • Create a manifest, or index, of a SWC package

Here are the most common attributes:

  • Inspectable declares that a component property appears in the Component Parameters panel in the authoring tool
  • Bindable declares that a component property is bindable using the Bindings tab of the Component Parameters panel

Here's an example of a bindable, inspectable property. It's excerpted from the Label component (mx.controls.Label):

[Inspectable(defaultValue="Label")]
[Bindable("writeonly")]
function get text():String
{
     return getText();
}
function set text(t:String):Void
{
     setText(t);
}
          

Find more information about attributes and using them in your components in Allen Ellison and Nigel Pegg's article, Building and Testing Components in Macromedia Flash MX 2004.