AS事件处理机制(重写 EventDispatcher 类)续

上一篇 / 下一篇  2009-04-16 11:21:31 / 个人分类:Flex

2.创建 EventDispatcher 成员,来获取此类中的方法.这样做的好处就是简化了代码.

示例:

 

[Copy to clipboard] [ - ]
CODE:
import AS2.events.EventDispatcher;
class yourClassName
{
    public var addEventListener:Function;
    public var removeEventListener:Function;
    public var dispatchEvent:Function;
    public var hasEventListener:Function;
   
    public function yourClassName()
    {
        var ed:EventDispatcher = new EventDispatcher(this);
        this.addEventListener = ed.addEventListener;
        this.removeEventListener = ed.removeEventListener;
        this.dispatchEvent = ed.dispatchEvent;
        this.hasEventListener = ed.hasEventListener;
    }
}

[3.简单的示例]
这只是一个简单的示例!
此示例文档详细:
Classes/AS2/events/MouseEvent.as
Example/AS2/events/EventDispatcher/EventDispatcherExample.as
Example/AS2/events/EventDispatcher/EventDispatcherExample.fla

MouseEvent 类定义鼠标事件.
打开 MouseEvent.as 文档.输入下面的代码:

 

[Copy to clipboard] [ - ]
CODE:
//----------------------------------------
import AS2.events.Event;
//----------------------------------------
class AS2.events.MouseEvent extends Event
{
    //----------------------------------------
    public static var CLICK:String = "click";
    public static var ROLL_OVER:String = "rollOver";
    public static var ROLL_OUT:String = "rollOut";
    //----------------------------------------
    private var className:String = "MouseEvent";
    //----------------------------------------
    public function MouseEvent(type:String)
    {
        super(type);
    }
    //----------------------------------------
}

保存文档.

EventDispatcherExample 类拥有 click(鼠标释放), rollOver(鼠标滑过), rollOut(鼠标滑离) 三个事件.
打开 EventDispatcherExample.as 文档.输入下面的代码:

 

[Copy to clipboard] [ - ]
CODE:
import AS2.utils.CFDelegate;
import AS2.events.MouseEvent;
import AS2.events.EventDispatcher;
class EventDispatcherExample extends EventDispatcher
{
    public function EventDispatcherExample(target:Object)
    {
        target.onRelease = CFDelegate.create(this, this.clickHandler);
        target.onRollOver = CFDelegate.create(this, this.rollOverHandler);
        target.onRollOut = CFDelegate.create(this, this.rollOutHandler);
    }
   
    public function toString():String
    {
        return "[object EventDispatcherExample]";
    }
   
    private function clickHandler():Void
    {
        this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
    }
    private function rollOverHandler():Void
    {
        this.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OVER));
    }
    private function rollOutHandler():Void
    {
        this.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
    }
}
保存文档.

打开 EventDispatcherExample.fla 文档.将图层 1 重命名为 "Actions",并再第一帧中输入下面的代码:

 

[Copy to clipboard] [ - ]
CODE:
import AS2.events.MouseEvent;
import EventDispatcherExample;
//绘制一个矩形.
this.createEmptyMovieClip("but", 1);
but.lineStyle(1, 0xCCCCCC, 100);
but.beginFill(0xF7F7F7, 100);
but.moveTo(10, 10);
but.lineTo(150, 10);
but.lineTo(150, 32);
but.lineTo(10, 32);
but.lineTo(10, 10);
but.endFill();
//创建一个文本域.
this.createTextField("txt", 2, 10, 10, 140, 22);
txt.textColor = 0x0099FF;
txt.selectable = false;
txt.autoSize = "center";
txt.text = "EventDispatcherExample";
//实例化 EventDispatcherExample 类.
var ex:EventDispatcherExample = new EventDispatcherExample(but);
//添加侦听器.
ex.addEventListener(MouseEvent.CLICK, clickHandler, this);
ex.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, this);
ex.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
//事件处理函数.
function clickHandler(evt:MouseEvent):Void
{
    trace("=======Click Event=======");
    trace("this --> " + this);
    //检查事件是否已添加事件侦听器.
    trace("hasEventListener click --> " + ex.hasEventListener(MouseEvent.CLICK));
    trace("hasEventListener rollOver --> " + ex.hasEventListener(MouseEvent.ROLL_OVER));
    trace("hasEventListener rollOut --> " + ex.hasEventListener(MouseEvent.ROLL_OUT));
    trace("Event Object --> " + evt);
    trace("");
    //移除事件侦听器.
    ex.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
}
function rollOverHandler(evt:MouseEvent):Void
{
    trace("=======RollOver Event=======");
    trace("this --> " + this);
    trace("hasEventListener click --> " + ex.hasEventListener(MouseEvent.CLICK));
    trace("hasEventListener rollOver --> " + ex.hasEventListener(MouseEvent.ROLL_OVER));
    trace("hasEventListener rollOut --> " + ex.hasEventListener(MouseEvent.ROLL_OUT));
    trace("Event Object --> " + evt);
    trace("");
}
function rollOutHandler(evt:MouseEvent):Void
{
    trace("=======RollOut Event=======");
    trace("this --> " + this);
    trace("hasEventListener click --> " + ex.hasEventListener(MouseEvent.CLICK));
    trace("hasEventListener rollOver --> " + ex.hasEventListener(MouseEvent.ROLL_OVER));
    trace("hasEventListener rollOut --> " + ex.hasEventListener(MouseEvent.ROLL_OUT));
    trace("Event Object --> " + evt);
    trace("");
    ex.removeEventListener(MouseEvent.CLICK, clickHandler);
    ex.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
}

保存文档.并测试文档.当鼠标滑过矩形, 点击矩形, 滑离矩形时.在输出面板中将会显示如图所示的内容:
点击在新窗口查看全图
CTRL+鼠标滚轮放大或缩小

[4.小结]
到此处,不知你了解多少.你自己知道吗? 有没有自己的想法?比如.为事件侦听器指定执行顺序等等.有想法
就应该去实现它,而不是纯粹的想.想而不做.那是没用的.
本人接触 AS3 不久.不过也有个大概的了解的.本人认为AS3 中的 EventDispatcher 类的工作原理与本文中
不尽相同.不过因此类已成内置类.难确认.其实现应该不会是常规的AS3 代码所编写的.


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

关于作者