|
|
|||||||||||||||||||||
| Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0 | |||||||||||||||||||||
|
|||||||||||||||||||||
|
|
ActionScript 2.0 |
|
|||||||||||||||||||
04. New keywords and features for OOPIt's time to get into the actual language features of ActionScript 2.0. As discussed earlier, object-oriented programming in ActionScript 1.0 involved more work than necessary. As you will soon see, with ActionScript 2.0, you don't have to spend time debating how best to do inheritance, where to place code and how to organize code libraries. Let's review the basics of OOP. The fundamental unit of OOP is an object, which contains two things: code and data. Because objects are supposed to be self-contained, this data and code (methods) are not necessarily for public consumption. As blackboxes, objects are responsible for managing their own data with their own methods. Objects communicate with other objects by sending messages; these messages are sent and received using public methods (also known as an API or Application Programming Interface) so that internal data is not manipulated directly. This is the basic idea of OOP. In object-oriented design, consider the big picture, think of the leading items and objects, and determine how these objects talk to each other. By breaking up the big picture into smaller units, and making each unit self-contained and self-responsible, an application can be built with managed ease. This idea of a unit is called a class in OOP, which ActionScript 2.0 officially supports. Here is the list of new keywords related to OOP:
We'll not duplicate Macromedia documentation and describe each of these keywords; instead, working examples will demonstrate what they do. Note: The keyword intrinsic is intended for Macromedia internal use only. There is no 'package' keyword, but the way import works with class paths is almost the same as packages in other languages, such as Java. Let's take a look at a simple example of setting up a class in ActionScript 1.0 (see Parent_AS1.fla), and its translation to ActionScript 2.0: // constructor
_global.Parent = function(name) {
this.init.apply(this, arguments);
};
// a class (static) property Parent.lastNames = new Array(); Parent.prototype.init = function(name) {
this.lName = name;
Parent.lastNames.push(name);
this.id = Parent.lastNames.length-1;
trace("Added '"+ Parent.lastNames[this.id] +"' at: "+ this.id);
};
Parent.prototype.getLastName = function() {
return (this.lName);
};
Parent.prototype.setLastName = function(s) {
this.lName = s;
Parent.lastNames[this.id] = s;
};
Parent.prototype.getNames = function() {
return (Parent.lastNames);
};
Parent.prototype.addProperty("lastName", Parent.prototype.getLastName,
The code above translated to ActionScript 2.0 would look something like this (refer to Parent.as): New keywords used in this example: class, private, public, static, get, set class Parent {
private var lName:String = "";
private var id:Number;
private static var lastNames:Array = new Array();
// constructor
public function Parent(name:String) {
init.apply(this, arguments);
}
private function init(name:String):Void {
lName = name;
lastNames.push(name);
id = lastNames.length-1;
trace("Added '"+ lastNames[id] +"' at: " + id);
}
public function get lastName():String {
return lName;
}
public function set lastName(s:String):Void {
lName = s;
lastNames[id] = s;
}
public function get names():Array {
return lastNames;
}
}
myFunction(myFirstArgument, mySecondArgument); The second way is to dynamically form the function reference using an object reference and funtion name string, as shown below: var myObjectReference = _root; var myFunctionName = "myFunction"; myObjectReference[myFunctionName] (myFirstArgument, mySecondArgument);The final method, and the one I've used above, is to use the apply() method of a function. When calling a function via apply(), you can define the object that the function is applied to (as the first argument) and pass in an array of arguments for the function (as the second argument). The first argument determines the scope of the function and the value that this will have for the function. The arguments passed to a function are available to the function in an array called arguments. var myObjectReference = _root; myArguments = [ myFirstArgument, mySecondArgument ]; myFunction.apply ( myObjectReference, myArguments ); Looking through the AS1.0 and AS2.0 implementation of the same class,
you can see that they are quite similar.
|
| - discuss this tutorial - |