painful stuff… trying to create a row of buttons, add eventListeners to them all, and pass the ‘release’ event to a single function (rather than one function per button). Because I have to use Delegate.create, it’s difficult to add arguments - and then also make sure that the removeEventListener also works. :oP
I’m SURE there are more efficient ways to do all this - and I don’t enjoy this MVC stuff at all when it results in this level of convolution. Makes me want to get back into the Flash IDE and stay on _root…
My starting point for this code came from paulspitzer.com. His suggestion of using arguments.caller as a ’shortcut’ in the removeEventListener works well - but only for the button that actually called the function… however, using this idea, I did find a way to pass button movieclip variables into the function. This means I can now add listeners to multiple buttons, pass arguments, and remove the listeners, with a fairly small amount of code.
Still seems like spaghetti though.
import mx.utils.Delegate
class Test_DelegateRemoval {
private var btn_frame:MovieClip;
private var releaseDelegate;
public function Test_DelegateRemoval() {
btn_frame = base.createEmptyMovieClip('btn_frame', 201);
releaseDelegate = Delegate.create(this, this.load_main);
for (var i:Number=0; i< =5; ++i) {
var btn_start = btn_frame.attachMovie('CW_button', 'btn_'+i, i);
btn_start.label = 'button '+i;
btn_start._x = i*100;
btn_start.btnID = i;
btn_start.addEventListener('release', this.releaseDelegate);
}
}
private function load_main(event:Object):Void {
for (var i:Number=0; i<=5; ++i) {
btn_frame['btn_'+i].removeEventListener('release', this.releaseDelegate);
}
var btnID:Number = event.target.btnID;
Init.load_data(fbtnID);
}
}