ActionScript 2.0

ActionScript 1.0   2.0     3.0  
Flash Player 4 5 6 7 8 9 10

Key Features

  • New class syntax
  • External .as files for class definitions
  • Class packages
  • Interfaces
  • Compile-time type checking

Summary

ActionScript 2.0 is the final step in the evolution of ActionScript 1.0. Though nothing much under the hood changed in Flash Player itself, the syntax for ActionScript source code and the way in which it was compiled into a SWF changed dramatically. With ActionScript 2.0 also came variable typing for ActionScript. This allowed you to specify the "type" of a variable, or what kind of data it would hold. These typing definitions allowed the compiler to more accurately check for errors helping improve the debugging process. Many of the changes made with ActionScript 2.0 were a precursor to what we saw with the introduction of ActionScript 3.0.

Why Use ActionScript 2.0

If you're using ActionScript 1.0 right now, why should you use ActionScript 2.0? There are two primary advantages to ActionScript 2.0. One is improved organization with ActionScript classes. The other is improved error checking and simplified debugging through the use of variable types. All around this usually means coding becomes easier.

When working with classes in ActionScript 1.0, class definitions were written as functions and class members were (typically) assigned to the function's prototype object. This process was not only confusing, but also lended itself to being disorganized as there was no standard location for classes to be defined. ActionScript 2.0 addresses these complications with a simple, structured class syntax that avoids the use of the term prototype in code and by requiring that classes be isolated into external .as files. Additionally, these class files can then be categorized further packages to improve organization and help prevent naming conflicts.

// ActionScript 2.0 class
// Saved as [arbitrary directory]/com/senocular/User.as
class com.senocular.User extends Object {
 
	private var name:String = "unknown";
 
	public function User(userName:String){
		name = userName;
	}
 
	public function getName(Void):String {
		return name;
	}
}
// ActionScript 1.0 equivalent
// Defined within a timeline script
var com = new Object();
com.senocular = new Object();
com.senocular.User = function(userName){
	this.name = userName;
}
 
com.senocular.User.prototype = new Object();
 
com.senocular.User.prototype.name = "unknown";
 
com.senocular.User.prototype.getName = function(){
	return this.name;
}

You may notice that, despite the additional confusion, the ActionScript 1.0 version of the class above is also defined without member access control (public and private statements) or strict data typing.

Users of ActionScript 1.0 often don't understand how difficult it is to identify problems in ActionScript, especially if they don't have experience in other, more mature languages. Often, when an ActionScript 1.0 project fails, it simply stops working. There is no indication from the compiler, or otherwise, that could help identify the point of failure. With strict data typing, ActionScript 2.0 helped improve this process. It allows variables and function parameters and return types to be associated with a specific value type. These associations allow the compiler to essentially double-check your code when you publish a SWF to make sure you're not using one type of variable in a place where another is expected.

var today:Date = new Date();
var result:Number = Math.cos(today); // cos expects a Number // Error: Type mismatch