Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0  
 
by Dave Yang, quantumwave.com - swfoo.com
Download Source Files 
 
ActionScript 2.0
 

01. Introduction
02. A little bit of OOP in ActionScript 1.0 
03. What's new in ActionScript 2.0?

04. New keywords and features for OOP
05. Scope and "this"
06. Private or protected?
07. Dynamic vs Static classes

08 Inheritance.
09. Overriding, Overwriting or Overloading?

10. Importing external class files
11. Interface
12. What is missing in ActionScript 2.0?
13. Conclusion

- discuss this tutorial -

07. Dynamic vs. Static classes

The dynamic class modifier allows a class to be extended (i.e. allows new properties and methods to be added). For JScript.NET programmers, this is the same as expando. For classes that are not dynamic (such as Math), the user cannot add new methods or properties to it. In ActionScript 1.0, it is quite common to see code that is added to the prototype object of a class, such as:

MovieClip.prototype.someNewMethod = function() {
// do something to the movieclip 
};
In ActionScript 2.0, this type of extension is not allowed. You can, however, create a subclass and add new methods and properties to it.

Here is an example of extending Math to create a new method called max() that finds the maximum value of any number of parameters (instead of being limited to two as in the original Math.max(), see Math2.as):

dynamic class Math2 extends Math {
   // store a reference to Math.max()
   private static var oMax:Function = Math.max;
// static class cannot be instantiated private function Math2() {} // override Math.max(), support any number of arguments public static function max():Number { var a:Array = arguments; var n:Number = a.length; if (n > 2) { var m:Number = oMax(a[n-2],a[n-1]); // remove two elements from the end of the array; // could use 'a.length -= 2' as a shortcut as well a.pop(); a.pop(); a.push(m); return Number(max.apply(null, a)); } else if (n == 2) { return oMax(a[0],a[1]); } else if (n == 1) { return a[0]; } else { return oMax(); } } }

Unlike the original Math class, I've chosen to make this class dynamic, so that it can be extended. Use this method like using other Math methods, except now it belongs to the Math2 class:
trace(Math2.max(70,6,3,12,82,9,28,5)); 

- discuss this tutorial -
 
©2003 Ultrashock.com - All rights reserved