用惯了AS3的事件机制,再回来用AS2的事件机制,怎么用怎么不爽。
特别是onRelease,onRollOver...这类鼠标事件,作用域好歹有个Delegate可以帮忙一下,最受不了的是,事件只能定义成一个函数。
好在AS2比较容易hack,随便写了个方法,让函数支持add,remove。用起来还有点像C#的感觉\~o~/
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 /**
2 * var obj = {_name:'obj'};
3 * obj.func = function(){trace(this._name);};
4 * with(this.createEmptyMovieClip('mc', 0))
5 * {beginFill(0x0,100),moveTo(0,0);lineTo(100,0);lineTo(100,100);lineTo(0,100);lineTo(0,0);endFill()};
6 * mc.num = 100;
7 * mc.onRelease = Delegate.create2();
8 * mc.onRelease.add( function(a,b){trace(a*b + this.num); }, 2,3 );
9 * mc.onRelease.add( function(){ trace("I am going to be removed
![](https://www.cnblogs.com/Images/dot.gif)
10 * mc.onRelease.add( Delegate.create(obj,obj.func) );
11 * // back to AS2
12 * mc.onRelease = function(){trace(this);}
13 */
14 public static function create2(Void):Function
15 {
16 var list:Array = [];
17 var delegate:Function = function()
18 {
19 for(var i=0; i<list.length; i+=2)
20 if(list[i]) list[i].apply( this, list[i+1] );
21 };
22 delegate.add = function( func:Function /*,
![](https://www.cnblogs.com/Images/dot.gif)
23 {
24 delegate.rmv( func );
25 list.push( func, arguments.slice(1) || null );
26 };
27 delegate.rmv = function( func:Function )
28 {
29 for(var i=0; i<list.length; i+=2)
30 if(list[i] == func)
31 list[i] = list[i+1] = null;
32 };
33
34 return delegate;
35 }
36
37