The Delegate class is very useful in large projects with many classes. I’ve read many posts of problems where scope in classes is lost through the dynamic MovieClip class and other classes and their various events, an example is
mc.onPress.
The Delegate class will solve this problem. Follow the class examples to understand the power of using this class in your applications.
The code below is an example of what happens when Delegate is
not used:
ActionScript Code:
// This class draws a clickable box on the screen
class Testdel {
// Private variables
private var mc:MovieClip;
private var number:Number = 5;
// Constructor function
public function Testdel() {
mc = _root.createEmptyMovieClip("mc", 0);
with (mc) {
lineStyle(1, 0xFF0000);
beginFill(0x00000, 0);
lineTo(0,0);
lineTo(100,0);
lineTo(100,100);
lineTo(0,100);
lineTo(0,0);
endFill();
}
mc.onPress = function() {
showNumber(); // Doesn't call
trace(number); // Traces undefined even though we defined it above as a private property
}
}
public function showNumber(Void):Void {
// Display the class property: number
trace (number);
}
}
To test the class place
var a:Testdel = new Testdel(); on the main timeline of a FLA in the same directory as the class. Notice in the class above where we have the function:
ActionScript Code:
mc.onPress = function() {
showNumber(); // Doesn't call
trace(number); // Traces undefined
}
When the MovieClip is pressed, it will not call
showNumber and number will trace undefined. Why? → The scope of the function is pointing at the MovieClip
mc not at the Class. We need to point the scope to the class. So the logical solution was to just use
local class variables to defeat the scope issue:
ActionScript Code:
var ScopeOfClass:Testdel = this;
mc.onPress = function() {
ScopeOfClass.showNumber(); // Now this will call the class method
trace(ScopeOfClass.number); // Now this will display 5 in the output panel
}
Now because we pointed the scope of the class to
ScopeOfClass local variable, it will call the function and display the trace statements in the output panel.
But! This is
not the most convenient way. What if you wanted to change 20 class variables or call more methods inside mc.onPress? Then you would have to copy and paste ScopeOfClass before every method and property! Not efficient at all.
How to get the same effect without copy and pasting ScopeOfClass?? By using
mx.utils.Delegate. Here is what the class looks like when you incorporate the Delegate class:
ActionScript Code:
import mx.utils.Delegate;
class Testdel
{
private var mc : MovieClip;
private var number : Number = 5;
public function Testdel ()
{
mc = _root.createEmptyMovieClip ("mc", 0);
with (mc)
{
lineStyle (1, 0xFF0000);
beginFill (0x00000, 0);
lineTo (0, 0);
lineTo (100, 0);
lineTo (100, 100);
lineTo (0, 100);
lineTo (0, 0);
endFill ();
}
mc.onPress = [b]Delegate.create (this, showNumber);[/b]
}
public function showNumber (Void) : Void
{
// Display the class property: number
trace (number);
}
}
Notice that the scope inside showNumber() points to the Class and not to the MovieClip.
Now to see it not working try this:
ActionScript Code:
mc.onPress = showNumber;
// Put this trace statement inside [b]showNumber()[/b]
trace(this); // Notice how this will now output the reference to the MovieClip, the scope is no longer pointing to the class
trace(number); that is inside
showNumber() will be
undefined because of the scope pointing to the MovieClip mc, instead of the Class.
For a
tutorial, visit the one here on ActionScript.org:
http://www.actionscript.org/tutorial...ss/index.shtml