Basic Outline for ActionScript Classes

Every ActionScript 3.0 class shares a basic structure or outline. You'll find a file in the shared directory called classOutline.as, which you can use as a template or starter for the classes you will write in this course.

Here is a sample class definition. Various sections of the code are identified by comments -- lines beginning with two slashes: //. We'll explain these sections, and how they work, below.

package
{
	//IMPORT BASIC FLASH CLASSES
	import flash.display.MovieClip;

	//CLASS BEGINS HERE
	public class helloWorldClass extends MovieClip
	{
		//DECLARE VARIABLES
		public var message:String;
		
		//CONSTRUCTOR
		public function helloWorldClass():void
		{
			message = "Hello Cool World!";
			sayHello();
		}
		
		//CUSTOM METHODS
		public function sayHello(): void
		{
			trace(message);
		}
		
	}

}

Context

First, let's understand that the script above is meant to be linked to a Movie Clip symbol defined within some main movie file (.fla). See our detailed instructions on how to do this.

Second, understand that the class defined above MUST be saved in an ActionScript file called helloWorldClass.as. That's because the name of the class must always match the name of its definition file. The name of the Constructor method within the class file must also match this name (see below).

Third, we assume that the Class field in the Linkage Properties dialog of the main movie has the value helloWorld.

Code

Let's start with the outermost wrapper, package{}. This is necessary to encapsulate our class definition for use within ActionScript 3's object environment. That's about all you need to know about it for the moment. Every class you write will have this feature.

Within the package container, we come to the import section. Here we use the import statement to link our class to any pre-existing classes on which we'll need to call. Since our example is meant to be attached to a Movie Clip symbol, we import flash.display.MovieClip, the basic class that defines Movie Clips.

Why do we have to bother with importing pre-defined Flash classes? (In ActionScript 2, it was not required.) Importation allows you to bring over only those framework classes you actually need. In the previous versions of ActionScript, lots of code was imported (i.e., written into the resulting .swf file) even though it was never used. Specific importing leads to smaller object files (.swf), and in large-scale Web operations, this is important.

After importation, we come to the start of the class definition, introduced by the keywords public and class. Because this class is defined as public, it is accessible to all other classes used by the current movie (though there are none).

We add the phrase extends MovieClip because our helloWorld class is meant to be linked to a Movie Clip symbol in the Flash library. This detail completes the connection we begin when we import flash.display.MovieClip, allowing our class to draw on all the native methods and properties of the pre-defined Movie Clip class.

Everything else in our class definition is contained within the class definition structure.

The next element is the declaration section. Here we use the var statement to declare (and initialize, if wanted) all the variables in our class that we intend to share among its various methods.

Next we come to the Contructor method for our class. Every ActionScript class must have a Constructor. This special method runs once, when the class is instantiated -- that is, whenever an object (Symbol) linked to this class is added to the current movie's display. For present purposes, this means the Constructor executes once, when the movie is launched. Later on, we'll come to cases when Constructors execute more than once, and on other occasions. For the moment, think of the Constructor as an initial or setup method. It establishes initial conditions for the class, and by extension, the object to which the class is linked.

As we've noted, the Constructor is a method, and a method is a function (note the use of that keyword in the Constructor). We can also create custom methods, which is to say, functions that are not executed when the class instantiates, but may be called upon flexibly, as needed.

Though our example doesn't need a custom method -- what it does could have been done simply through the Constructor -- we've included one for the sake of illustration.

Formally, a custom method looks exactly like the Constructor. It differs only in name. Remember, the name of the Constructor must exactly match three other occurrences:

Needless to say, most class files are considerably more complex; but this one does what it's meant to do. To see what that is (if you can't already guess), download the helloWorld folder from the shared account on student-iat and give it a try.


University of Baltimore Logo

Last updated: 02/14/08 16:40:20
Copyright © 2008 School of Information Arts and Technologies