as3 创建遮罩层和自定义带参数的事件
package { import flash.events.Event; /** * ... * @author chb */ public class MyEvent extends Event { public static const CLICK:String = "click";//常量一定要大写 private var _number:Number; public function MyEvent(type:String){ super(type); } //参数,可以有很多个我只写了一个 public function set number(n:Number):void { _number = n; } public function get number():Number { return _number; } //重写toString方法,这个具体还不太明白但是公司的前辈说要这样写,没添加一个参数方法则在"eventPhase"后面加一个,我现在加的是"number" override public function toString():String { return formatToString("ShowDriftMsgEvent", "type", "bubbles", "cancelable", "eventPhase", "number"); } } }
package { import flash.display.Sprite; import flash.display.Graphics; import flash.events.EventDispatcher; import flash.events.Event; import flash.events.MouseEvent; import flash.ui.Mouse; /** * ... * @author chb */ public class chb extends Sprite { //使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知 private var _myED:EventDispatcher; public function chb():void { trace("进入构造方法"); _myED = new EventDispatcher(); setMain(); } public function setMain():void { //用程序绘画一个显示元件 var mc_show:Sprite = new Sprite(); var g:Graphics = mc_show.graphics; g.lineStyle(0, 0, 1); g.beginFill(0xffd700, 1); g.drawRect(0, 0, 100, 100); g.endFill(); this.addChild(mc_show); //用程序绘画一个遮罩元件(遮罩层的背景颜色如何没有关系,但是大小和坐标一定要跟被遮罩的元件一样,这样才能完全遮罩住) var mc_mask:Sprite = new Sprite(); var g:Graphics = mc_mask.graphics; g.lineStyle(0, 0, 1); g.beginFill(0xffffff, 1); g.drawRect(0, 0, 100, 100); g.endFill(); this.addChild(mc_mask); //设置遮罩元件遮罩显示元件 mc_show.mask = mc_mask; //添加显示元件点击事件并发送自定义事件 mc_show.addEventListener(MouseEvent.CLICK, clickHandle); //监听自定义事件 this._myED.addEventListener(MyEvent.CLICK,myEventClickHandle); } private function clickHandle(e:MouseEvent):void { //创建一个自定义事件对象 var mye:MyEvent = new MyEvent(MyEvent.CLICK); mye.number = 10; //发送带参数的自定义事件 this._myED.dispatchEvent(mye); } private function myEventClickHandle(e:MyEvent):void { trace("自定义事件发送过来的值为:"+e.number); } } }
人生没有失败,只有自我放弃;只要不放弃,梦想总有一天会变成现实。