Flash 2004 MX includes an updated version of the Actionscript language which now enables developers to use
object oriented programming. Actionscript 2.0
includes new keywords such as class, impliments, extends and interface, which developers of Java might be familiar
with. You can now also declare the object types (eg, String, Number, Boolean) of variables when you create them.
Below is a quick rundown of the new keywords and examples of how to use them.
Class
This keyword defines a class. A class is a kind of custom object which can contain methods (ie functions) and
properties (i.e. local variables) that you can specify and modify in each instance of this class. Using classes is a
little like creating symbols in the library, with the exception that when you change instances of a symbol, you also
alter the symbol itself. Classes are different in that you can make local changes to instances without affecting
the class.
When working with classes, the code for the class is not stored in the .fla as it is in
actionscript 1.0, but rather the code is stored in an external .as file ('as' stands for 'actionscript') that has
the same name as the class defined inside it. If the file is nested in a directory, then the code that creates the class
needs to reflect this. For example,if the file is in a directory called animals/mammals/cat.as then the class
declaration (the first line of the code that defines that class) may look like the example below. All variables created
in a class are defined by the keyword 'var' then a colon is used to seperate the variable and the value. You can
optionally include the object type keyword to ensure that your variable is of a particular type. These keywords can be
String, Number, Boolean (true or false), null or undefined. These are referred to as 'primitive data types'. You can also
define the data type of the return value of functions as the example below demonstrates:
class animals.mammals.cat {
var legs: Number 4;
var sound: String 'meow';
var eats: String 'cat food';
function getSound(): String {return sound};
function getFood(): String {return eats};
}
Extends
This keyword defines a class as a subclass of another class, for example, if you create a class called 'cat' and
you want to create a new class called 'tiger' you can make the tiger a subclass of cat. In this case, cat becomes the
'superclass' of tiger. Note multiple inheritance is not supported in Actionscript 2.0. If you want to include more
than one superclass, then you will need to create a 'super super class' and have the superclass inherit from that one.
class tiger extends cat {
var sound: 'rrrraaarrrr';
var eats: 'people';
}
class bengalTiger extends tiger {
var sound: 'rearrrowwww';
}
Interface
Interfaces are similar to classes, with the exception that it can only contain function declarations which are not
defined in this class, but are defined in any class that inherits the interface. As with classes, interfaces are
stored in a seperate .as file with the same name as the name of the interface.
Interfaces are useful when creating several classes that use the same methods and require the same types of
information to be passed around. An example of an interface is this:
interface getFood {
function findFood():Boolean;
}
Impliments
Impliments is a keyword that allows classes to inherit interfaces. An example of a class which impliments an
interface is show below:
class cat impliments getFood {
var claws: Boolean true;
var nocturnal: Booloean true;
var prey: String 'mice';
function findFood(): Boolean {
if (house.hasPrey(prey){
return true;
} else {
return false;
}
}
Dynamic
The dynamic keyword allows classes to access and add to dynamic content created at runtime - for example, you can add
new properties to a class that are not defined in the class's constructor (the code that creates the class in the
first place). The example below demonstrates how new properties and functions can be added to an instance of a
dynamic class.
dynamic class house{
var rooms: Number;
var isDark: Booloean;
}
var myHouse = new house();
myHouse.contains = 'mice';
myHouse.hasPrey = function (String prey){
if (house.contains == prey) {
return true;
} else {
return false;
}
}
Static
if a variable in a class is defined as static then the variable is only created once per class, and not in any object
based on that class:
class credits {
static var author: String 'Myke Black';
static function getAuthor(): String {return author};
}
Public
A class declared as public is available to any classes that wish to reference it. All variables and functions are
public by default, but the keyword can be included for those more familiar with java/c++ programming styles
Private
In contrast to public, any variable or function that is declared as private is only available to classes that create
them and subclasses that are inherited from the superclass in which the methods and properties are declared.
Import
The import keyword allows you to reference classes without using the full qualified name. In the example about, the
class animals.mammals.cat can be more easily handled but using this code:
// method 1 - using the class without importing
var myCat:tiger = new animal.mammal.cat.tiger();
// method 2 - using the class by importing
import animal.mammal.cat.tiger;
var myCat:tiger = new tiger();
Multiple scripts can be imported using wildcards. This means that you can quickly reference multiple classes
by importing at a higher folder level:
import animal.mammal.*;
var myCat:tiger = new tiger();
var myDog:wolf = new wolf();
Summary
The new methods of Actionscript 2.0 provide powerful programming tools for developers, while still supporting actionscript
1.0 code. You cannot combine 2.0 and 1.0 code in the same application, but you can import swfs which use a combination
of these scripting versions as self contained movie objects.
Some final points to remember are:
-
ActionScript 2.0 scripts are stored as external script files. Each class defined in a seperate .as file.
Classes and interfaces cannot be defined in the movie itself.
-
Applications developed with ActionScript 2.0 are supported by Flash Player 6 and later.
-
Flash does not generate compiler errors if you use actionscript 2.0 but publish the swf as actionscript 1.0, so be wary of this.
More Games