org.as2lib.env.reflect.Delegate
Delegate
offers with Delegate.createDelegate a method for
using old-style Template classes.
Using event handling like in common MovieClips creates problems in OOP
ActionScript due to the fact that it uses functions as event executions
without minding about that the scope always refers to the MovieClip.
Delegate.createDelegate
allows a proper Workaround for the redirection
of such methods to a proper different scope.
Example:
Test class:
class com.domain.MyMovieClipController {
private var content:String;
public function MyMovieClipController(content:String) {
this.content = content;
}
public function onEnterFrame() {
trace(content);
}
}
Usage:
import com.domain.MyMovieClipController;
import org.as2lib.env.reflect.Delegate;
var mc:MyMovieClipController = new MyMovieClipController("Hello World!");
// Following will not work because of the wrong scope
_root.onEnterFrame = mc.onEnterFrame;
// Workaround using delegate
_root.onEnterFrame = Delegate.createDelegate(mc, onEnterFrame);
static public function createDelegate(scope, method:Function):Function
Creates a method that delegates its arguments to a certain scope.
scope | Scope to be used by calling this method. |
method | Method to be executed at the scope. |
Function that delegates its call to a different scope & method.
static public function createExtendedDelegate(scope, method:Function, args:Array):Function
Creates a method that delegates its arguments to a certain scope and uses additional fixed arguments.
Example:
import org.as2lib.env.reflect.Delegate;
function test(a:String, b:Number, c:String) {
trace(a+","+b+","+c);
}
var delegate:Function = Delegate.createExtendedDelegate(this, test, ["a"]);
delegate(1,"b"); // will trace "a,1,b"
scope | Scope to be used by calling this method. |
method | Method to be executed at the scope. |
args | Arguments to be used at first position. |
Function that delegates its call to a different scope & method.