Flex中一个鼠标事件多处响应的处理

希望通过在界面上,鼠标移动啊、点击啊能激发多个处理监听器,我是这么计划的:先弄个统一的界面,这里用的是一个canvas,监听鼠标事件,然后将鼠标事件进行转发,通过dispatchEvent,将消息转发出去(这里就要求所有被转发的地方实现事件监听器,继承EventDispatch即可。)

步骤1:添加鼠标事件监听器:

public function MapOperateCanvas()
        {
            super();
            this.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandler);
            this.addEventListener(MouseEvent.MOUSE_UP,mouseHandler);
            this.addEventListener(MouseEvent.MOUSE_OVER,mouseHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT,mouseHandler);
            this.addEventListener(MouseEvent.MOUSE_MOVE,mouseHandler);         
        }

步骤2:添加鼠标事件转发

/**
         * 设置鼠标事件处理
         * */
        private function mouseHandler(e:MouseEvent){
            e.localX = e.stageX;
            e.localY = e.stageY;
            if(e.type == MouseEvent.MOUSE_DOWN){
                var p:Point = stage.localToGlobal(new Point(e.localX,e.localY));
                trace(p);
                trace(e);
            }
            _maps.receiveMouseEvent(e);   
            _model.dispatchEvent(e);
        }

步骤3:对应的被转发的对象处理相应的消息

具体处理就是_model实例的类中会有相应的方法。这里就不细说了。

 

该文件的源码如下:

package com.ztesoft.map.view { import com.earthplayer.maps.MapCanvas; import flash.events.MouseEvent; import flash.geom.Point; import mx.containers.Canvas; public class MapOperateCanvas extends Canvas { private static var _model:ModelCanvas = new ModelCanvas();//模型面板 private static var _maps:MapCanvas = new MapCanvas();//地图面板 public function MapOperateCanvas() { super(); this.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandler); this.addEventListener(MouseEvent.MOUSE_UP,mouseHandler); this.addEventListener(MouseEvent.MOUSE_OVER,mouseHandler); this.addEventListener(MouseEvent.MOUSE_OUT,mouseHandler); this.addEventListener(MouseEvent.MOUSE_MOVE,mouseHandler); } /** * 设置鼠标事件处理 * */ private function mouseHandler(e:MouseEvent){ e.localX = e.stageX; e.localY = e.stageY; if(e.type == MouseEvent.MOUSE_DOWN){ var p:Point = stage.localToGlobal(new Point(e.localX,e.localY)); trace(p); trace(e); } _maps.receiveMouseEvent(e); _model.dispatchEvent(e); } /** * 设置地图面板 * */ public function set mapCanvas(m:MapCanvas):void{ _maps = m; } /** * 设置地图面板 * */ public function set modelCanvas(m:ModelCanvas):void{ _model = m; } } } 

你可能感兴趣的:(Flex中一个鼠标事件多处响应的处理)