In the upper two, we studied the use, origin, implementation, and advantage of the delegate of Macromedia Updater 2. However, this delegate can be well implemented a proxy method. But many times, not this, more things need to know what happened, then call your own processing function. For example, when I press it, it is possible to change my title ChangeText () after a Label component knows this incident, and it is also possible that a MediaPlayback component knows that after this event is to play a FLV video play () ... With Delegate, you can only use a function to proxy these transactions. However, these transactions are methods for different objects, and the ChangeText () method is that the Play () method is a MediaPlayback object. Of course, these can be placed in one method, as follows: function eevnthandler () {lb.change (); mpb.play ();} But many times these methods cannot be written in an EventHandler function, then we think of EventDispatcher, He allows an object to increase the listener and then broadcast events to each listener by this object. These listeners listen to these events each call their own handling events. But the biggest shortcomings of Macromedia's EventDispatcher is that there are no passages that have occurred in many events, such as the mouse point, many times the position of the mouse is required, which is the button is pressed. EventDispatcher has no good norm to pass. The advantages of combination. I roughly conceive the model of multicast entrusted. Change Delegate to EventHandler, the constructor is slightly changed: // ******************************************* ************************************************ @FileName EventHander.as// @ Package com.flashvan.event // @Description event agent handle // @author aol @Email jeremy1982@21cn.com/ @create 2004.10.21 @LastChange 2004.10.21 @History // *** *********************************************************** **********************
Import com.flashvan.event.eventargs; import com.flashvan.event.except.exception;
class com.flashvan.event.EventHandler extends Object {/ ** Creates a functions wrapper for the original function so that it runs in the provided context. @parameter obj Context in which to run the function. @paramater func Function to run. * / Static function create (Obj: Object, func: function): function {var f = function () {var target = arguments.callee.target; var func = arguments.callee.func; // // i (typeof (Arguments) [0])! = "Object" && (Arguments [1] instanceof evenetargs) // throw new exception ("Parameter is met [Sender: Object, E: Eventargs], this, [target, func]) Return Func.Apply (Target, arguments);}; f.target = Obj; f.Func = func;
Return f;
Function EventHandler (o: Object, f: function) {OBJ = O FUNC = f;}
Private var func: function; private var obj: object;
Function CreateEventLer (): function {return create (Obj, func);}} A special logging event occurs related information Eventargs:
// ******************************************************** *************************************** / / @FileName Eventargs.as// @Package com.flashvan.event @description event parameter Class (this is passed after the event occurs) @Author aol @Email jeremy1982@21cn.com/ @create 2004.10.21 @Lastchange 2004.10.21 @History // ****** *********************************************************** *******************
Class com.flashvan.event.eventargs {function Eventargs () {}; public static function get empty (): Eventargs {return new Eventargs ();} public function medin (): void {delete this;}}; inherited by Eventarg Down, for example, mouseevntargsimport com.flashvan.event.eventaTARGS specifically for mouse events;
Class com.flashvan.component.core.mouseeventargs Extends Eventargs {Private Var _Clicks: Number; // The mouse is pressed // Which button // 0 is not pressed, 1 is left button, 2 is right click For the roller // to monitor all the unapplicared functions ^ _ ^ private var _Button: Number; private var _x, _y: number; // mouse position private var _delta: Number; // Roller location private var _springTime: Number; // time to trigger public function MouseEventArgs (button: Number, clicks: Number, x: Number, y: Number, delta: Number) {_springTime = getTimer (); _button = button; _clicks = clicks; _x = x _Y = y; _delta = (delta == null)? 0: Delta;} public function get x (): Number {return_x;} public function get y (): Number {return_y;} public function Get ClickS ): Number {return _clicks;} public function get Button (): Number {return _clicks;} public function get Delta (): Number {return _delta;} // time to trigger public function get SpringTime (): Number {return _springTime; }} To implement multicast interfaces to achieve this column: import com.flashvan.event.eventargs; import com.flashvan.collections.arraylist; import com.flashvan.event.eventhandler; i nterface com.flashvan.event.IMulticastEventDelegate {// public function AddEventHandler (o: Object, f: Function): Void; public function AddEventHandler (eh: EventHandler): Void; public function getInvocationList (): ArrayList; // public function RemoveEventHandler (o: Object, f: function): Void; public function RemoveEventHandler (eh: EventHandler): Void; public function RemoveAllEventHandler (): Void; public function InvokeAllEventHandler (sender: Object, e: EventArgs): Void;
}
The main EVENT class implements this interface:
import com.flashvan.event.IMulticastEventDelegate; import com.flashvan.collections.ArrayList; import com.flashvan.collections.IEnumerator; import com.flashvan.event.EventArgs; import com.flashvan.event.EventHandler;
class com.flashvan.event.Event implements IMulticastEventDelegate {private var invocationlist: ArrayList; public function Event () {invocationlist = new ArrayList ();} // public function AddEventHandler (o: Object, f: Function): Void // { // invocationList.add ({Object: O, Method: f}); //} // public function RemoveEventHandler (o: object, f: function): void // {// invocationList.remove ({Object: O, method: f}); //} public function AddEventHandler (eh: EventHandler): Void {invocationlist.Add (eh);} public function RemoveEventHandler (eh: EventHandler): Void {invocationlist.Remove (eh);} public function RemoveAllEventHandler (): Void {invocationlist.Clear ();} public function getInvocationList (): ArrayList {return invocationlist;} // public function InvokeAllEventHandler (sender: Object, e: EventArgs): Void // {// var enumerator: IEnumerator = InvocationList.Getenumerator (); // while (enumerator.movenext ()) // {// var obj: object = enumerator.getCurrent (). Object; // var f: function = enumerator. GetCurrent (). Method; // f.apply (Obj, [Sender, E]); //} //}
public function InvokeAllEventHandler (sender: Object, e: EventArgs): Void {var enumerator: IEnumerator = invocationlist.GetEnumerator (); while (enumerator.MoveNext ()) {enumerator.getCurrent () createEventHandler () (sender, e);. }}}
The following is tested with a test component: import com.flashvan.event.event; import com.flashvan.event.EventArgs; import com.flashvan.event.eventhandler;
Import com.flashvan.component.core.component; import com.flashvan.component.core.mouseeventargs;
class com.flashvan.test.EventTestComponent extends Component {public var MouseMove: Event; public var MouseDown: Event; public function EventTestComponent () {MouseMove = new Event ();} public function onMouseMove (): Void {var e: MouseEventArgs = New mouseeventargs (0, 0, _xmouse, _ymouse); MouseMove.Invokealleventhandler (this, e);}} The top of the above is already packaged, including the above test components, in fact, the user code only needs these:
Create a new FLA file, drag the doing EventTestComponent to the scene to name ETC.
Create a new class com.flashvan.test.Eventtest: import com.flashvan.event.eventhandler;
Import com.flashvan.component.core.component; import com.flashvan.component.core.mouseeventargs;
Import com.flashvan.test.EventtestComponent;
class com.flashvan.test.EventTest extends Component {private var etc: EventTestComponent; public function EventDelegate (sender: Object, e: MouseEventArgs): Void {trace (sender);} public function EventDelegate2 (sender: Object, e: MouseEventArgs) : Void {trace (e.SpringTime);} public function EventTest (c: EventTestComponent) {etc = c; etc.MouseMove.AddEventHandler (new EventHandler (this, EventDelegate)); etc.MouseMove.AddEventHandler (new EventHandler (this, EventDelegate2);} public static function main (): void {new eventtest (eventTestComponent);}}
FLA file main timeline plus com.flashvan.test.eventtest.main (etc); Ctrl Enter test, as long as the mouse movement will appear as follows: _LEVEL0.ETC72_LEVEL0.ETC1563