自定义事件类
package lu.leon.events { import flash.events.Event; /** * ... * @author LeonLu */ public class PicChosenEvent extends Event { public static const CHOSEN:String = "chosen"; public var id:int; public function PicChosenEvent(type:String, id_:int, bubbles:Boolean = false, cancelable:Boolean = false){ this.id = id_; super(type, bubbles, cancelable); } } }
该自定义事件中,定义了2个属性:CHOSEN、id,其中,CHOSEN是以后在实例化PicChosenEvent时需要侦听的事件类型(也可以理解为需要PicChosenEvent干的事情类型),id则是自定义事件的目的所在,侦听当发生自定义的事件类型CHOSEN时,返回id,即:e.id,此处的e是PicchosenEvent的实例。
pic.addEventListener(MouseEvent.CLICK, picClick, false, 0, true); function picClick(e:MouseEvent):void { var currentSmallPic:MobileSmallPicItem = e.currentTarget as MobileSmallPicItem; if (currentSmallPic != oldSmallPic) { if (oldSmallPic) { oldSmallPic.gotoAndStop(1); } currentSmallPic.gotoAndStop(2); oldSmallPic = currentSmallPic; //为PicChosenEvent构造函数传入2个参数:事件类型PicChosenEvent.CHOSEN和oldSmallPic.id, //其中PicChosenEvent.CHOSEN不能省略PicChosenEvent,否则就是指当前类的CHOSEN属性了 dispatchEvent(new PicChosenEvent(PicChosenEvent.CHOSEN, oldSmallPic.id)); } }
smallPicsBrowser.addEventListener(PicChosenEvent.CHOSEN, smallPicChosen, false, 0, true); function smallPicChosen(e:PicChosenEvent):void{ trace(e.id); }