关于事件机制的理解
最近终于把事件机制弄明白了。和大家分享一下。
下面是定义的事件:
package { import flash.events.Event; public class NBEEvent extends Event { /** * 对 */ public static const RIGHT: String = "nbe_event_right"; public function NBEEvent(type: String, bubbles: Boolean = true, cancelable: Boolean = false) { super(type, bubbles, cancelable); } public override function clone(): Event { return new NBEEvent(type, bubbles, cancelable); } public override function toString(): String { return formatToString("NBEEvent", "type", "bubbles", "cancelable", "eventPhase"); } } }
然后用一个按钮来发送事件
import NBEEvent; import flash.events.MouseEvent; btn.addEventListener(MouseEvent.CLICK,onClick); function onClick(e:MouseEvent) { dispatchEvent(new NBEEvent(NBEEvent.RIGHT,false,false)); }
用一个mc来接收事件
import NBEEvent; m1.addEventListener(NBEEvent.RIGHT,_onRW,false,0,true); function _onRW(e:NBEEvent) { trace(e.type) }