Tight binding with Delegate. Evil but sweet. Dirty but fast.
First perhaps I should explain what I am talking about. One of the core OOP principles is a principle of a class being a “black box”. Or class’s encapsulation. Which means , it’s advisable not to expose inner working of a class , i.e. it’s methods, variables etc to the outside word. Instead , to communicate with that class , to choose a few clearly and carefully designed public methods , something being referred to as classes interface. In AS2 this is done of course by declaring the methods and variables as “private”.
Although ,majority of time, these are very good rules since there is a good reason behind them, on occasion it’s fine to break them providing the benefits of breaking them outweighs the consequences of doing so.
I discovered the method that enables very effective class 2 class communication. The trick is to define the function variable on a class that is being attached and delegate that functionality back to the main class, using Delegate class from package mx.utils.
Let’s consider this simple scenario. We have a class called MainView, that extends MovieClip. That class attaches a movie clip associated with a class called : ConfirmationPopUp.
All confirmation class does, is have an OK button user needs to click, which will close the popUp / remove the movieClip.
On the ConfirmationPanelClass, we have 2 essential lines of code :
1. A class variable is declared of the type “Function”.
2. Button has onRelease handler defined that calls that variable via Delegate.
example :
class ConfirmationPanel extends MovieClip {
private var onOKpressed :Function; //define function as variable
private var okButton : MovieClip;
function ConfirmationPanel ()
{
//when okButton is pressed, the onOKpressed is called
okButton.onRelease = Delegate.create(this,onOKpressed);
}
}
On the mainView class, all that is needed is when attaching the pop up movie clip , is to connect a onOKpressed variable with a function on a mainView class. Which in this case would be closePopUp function. Here’s the code :
function attachPopUp {
popUp = attachMovie("ConfirmationPopUp","popUp",1,{
onOKpressed:Delegate.create(this,closePopUp)
});
}
function closePopUp():Void {
popUp.removeMovieClip();
}
The main advantage of this code, is that is very efficient and simple. Also it help to shift the functionality outside the movie clip reducing it to a host of interactive elements, but all the interactivity implementation lies outside of this simple view.
The variation of this approach is to not to use Delegate to call the var function (onOKpressed), but call the var function inside of another function.
for example :
function onRelease()
{
super.onRelease();
onOKpressed();
doSomeOtherStuff();
}
if it’s used like this, it enables the class to sent some arguments to the mainView class. This can be used for example if we have 2 buttons, “OK” and “Cancel”. Both will close the pop up , but main class needs to know user selection. So on main view we will change the code to :
function closePopUp(accepted:Boolean):Void {
if (accepted){
//do something
} else {
//do something else
}
popUp.removeMovieClip();
}
and on the ConfirmationPopUp :
//first define the button functionalities
okButton.onRelease = Delegate.create(this,onAccept);
cancelButton.onRelease = Delegate.create(this,onCancel);
function onAccept():Void {
onOKpressed(true);
}
function onCancel():Void {
onOKpressed(false);
}
see demo :
or , if you use DelegateExt class, even a simpler/faster way to do this would be :
okButton.onRelease = DelegateExt.create(this,onOKpressed,true);
cancelButton.onRelease = Delegate.create(this,onOKpressed,false);
Now with some mid size to bigger size flash application which needs al kinds of dialog panels, pop ups etc, I’d go for a more robust / reusable approach when I’d build the event driven base pop up component, have confirmationPopUp class extend it and probably used EventDispatcher for communication between the classes.
But lets say, this is a small game, this is only one situation where any pop up is going to be used, maybe a small pop up thanking user for registration. The budget is tight and applying some previously build robust framework would perhaps be waste of time and may be a case of killing the fly with the machine gun.
So perhaps in this case , the few lines of code needed to implement this is a benefit that outweighs the fact that the main class needs to know the name of the function variable when it’s attaching the movie clip.

