Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Working with Classes

A class is a special reserved keyword that allows you to add ActionScript to interact with the Flash Movie. You can think of a class as a category for an object. In the real world, you can think of apples as a category of the class called Fruit. Or Human Resources as a category of the class called Departments. Each class contains the information you need to construct programs.

By default, ActionScript has a number of classes built into it. An example of a class file is the Movie class. With the Movie class, you can control the presentation, playback, and effects of a movie clip.

For instance, the following ActionScript lets you go to a specific frame in a movie and begin playing the clip:

MyMovie.gotoAndPlay(30);

The MovieClip class is one of the Core classes built into Flash. The others are

  • Accessibility

  • Arguments

  • Array

  • Boolean

  • Button

  • Camera

  • Color

  • ContextMenu

  • ContextMenuItem

  • Date

  • Error

  • Function

  • Key

  • LoadVars

  • LocalConnection

  • Math

  • Microphone

  • Mouse

  • MovieClip

  • MovieClipLoader

  • NetConnection

  • NetStream

  • Number

  • Object

  • PrintJob

  • Selection

  • SharedObject

  • Sound

  • Stage

  • String

  • System

  • TextField

  • TextFormat

  • Video

  • XML

  • XMLNode

  • XMLSocket

A class is essentially a master document that abstracts keywords that you can use in your programming. The class is a specialized document that explains how particular things execute. Through adding additional keywords, such as gotoAndPlay, you can call specific features within the class from your main movie through ActionScript.

With Flash MX 2004, you can now more easily create your own custom classes. This is essential when you are building large systems that can benefit from small applications being linked together. The class is the small application, and the linking is done through ActionScript.

There are two parts of creating and using a class: creating the class file and linking ActionScript in a movie to the class file.

You create the class file by creating a new ActionScript (AS) file. Open Flash MX 2004 Professional and choose ActionScript File.

You will create a class called Animals. Because of the way Flash works, you need to name the new AS file Animals.as. The filename and the class must be identical. Save the file to a new folder called AnimalClass.

In the Animals.as file, begin by opening the class using the Class method. This is called the declaration:

class Animals{
}

Now you need to add some variables that can be called later:

class Animals{
var commonName:String;
var species:String;
var currentCount:Number;
}

Here, the Animals class will collect the common name for an animal (commonName), the species it belongs to (mammal, fish, insect, and so on) and how many are currently in existence (currentCount). The commonName and species will be as Strings, whereas the currentCount will be a definite number, so it has been declared as a Number.

Finally, you will add a function that takes the values in the Variables and past them into a format usable by the Flash Player.

Add the following:

class Animals{
var commonName:String;
var species:String;
var currentCount:Number;

Function animalText:String {
Return(commonName + " is a member of the " + species +" family, of
 which there are only "+ currentCount + "left in the world.";
}
}

The class now has the variables and the return value for those variables. All it needs now is the content. Save the file.

Now, in the same folder as the class file, create a new Flash Movie and call it classContent.fla.

Open the classContent.fla file and open the Actions Panel.

Enter the following:

var lion:Animals = new Animals("Lion", "Mammal", 264000);
var goldfish:Animals = new Animals("Gold Fish", "Fish", 1000000000);

The Flash movie knows to look to the same directory for the information on how to use the information. Each variable links to the Animals class with animal name, species and count.

You can now add a trace to show that variables and the class are linking together.

trace (lion.animalText());
trace (goldfish.animalText());

The result posted to the Output Panel is

Lion is a member of the Mammal family, of which there are only 264000 left in the world.

Gold Fish is a member of the Fish family, of which there are only 1000000000 left in the world.

This is only an introduction to classes. There is a lot more you can explore, but classes can significantly help the way you develop your ActionScript codes.

Strict Data Typing

An enhanced feature within ActionScript that is necessary to control the flow of data through your programs is strict data handling of variables. When you create a variable, it used to be that Flash could differentiate only between an expression and a string. Now you can define the specific data type. For instance, the following defines a variable as a date data type:

var myDate:Date;

Using strict data typing will help reduce the amount of code you write by exactly assigning the value of the data type to the variable. The following list identifies all of the data types you can use:

  • Array

  • Button

  • Camera

  • Color

  • ContextMenu

  • ContextMenuItem

  • Date

  • Error

  • LoadVars

  • LocalConnection

  • Microphone

  • MovieClip

  • MovieClipLoader

  • PrintJob

  • NetConnection

  • NetStream

  • SharedObject

  • Sound

  • String

  • TextField

  • TextFormat

  • Video

  • XML

  • XMLNode

  • XmlSocket

By using strict data typing, you can eliminate any confusion about what the perceived value of a variable actually is.

Exception Handling

When you add code to your movie, sometimes you might receive an error; for example, if you are loading an external XML file. What happens if the file is simply not there?

The try...catch...finally code block allows you to manage what are called exceptions, which have been part of JavaScript and programming languages such as Java and C# for quite some time.

You will use try...catch...finally with classes you create. A try function opens a block of code that looks to validate a section of code. If the code does not validate on the first try block, you can move the code through a second try block. You can add many try blocks. If an exception is thrown by the try block, control is then passed to the next catch block. At the end of your block sequence, you can add a Finally statement that will execute if the try block does not reach an exception.

In the following example, the ActionScript uses a try block to find an exception. When one is met, a catch block is executed. If nothing happens, the finally block throws an exception.

var userName = new Employee();
try {
    var returnVal = userName.getEmployeeInfo();
    if (returnVal != 0) {
        throw new Error("Can not find information on employee.");
    }
} catch (e) {
    status_txt.text = e.toString();
} finally {
    // Delete the 'Employee' object no matter what.
    if (userName != null) {
     delete userName;
    }
}

For this block to work, you need to work with a class called Employee.

As you can see, the try...catch...finally block enables you to add provisions to your code that protect your movie from events that can break your code. In other words, you can ensure against exceptions.

  • + Share This
  • 🔖 Save To Your Account