• AS3自定义鼠标光标后应注意鼠标事件捕获问题


       AS3自定义鼠标光标很容易,在帮助中就有这样的代码:

    AS3自定义鼠标光标
    var cursor:Sprite = new Sprite();
    cursor.graphics.beginFill(
    0x000000);
    cursor.graphics.drawCircle(
    0,0,20);
    cursor.graphics.endFill();
    addChild(cursor);

    stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
    Mouse.hide();

    function redrawCursor(event:MouseEvent):void
    {
        cursor.x 
    = event.stageX;
        cursor.y 
    = event.stageY;
    }

      但我们实际的项目不可能简单到没有其他的影片剪辑,假如出现下面的代码:

    var mc:Sprite = new Sprite();
    mc.graphics.beginFill(
    0xFF0000);
    mc.graphics.drawRect(
    200,200,100,100);
    mc.graphics.endFill();
    addChild(mc);
    mc.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
    function overHandler(event:MouseEvent):void
    {
        trace(
    "我不是每次碰到都显示!!");
    }

    var cursor:Sprite = new Sprite();
    cursor.graphics.beginFill(
    0x000000);
    cursor.graphics.drawCircle(
    0,0,20);
    cursor.graphics.endFill();
    addChild(cursor);

    stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
    Mouse.hide();

    function redrawCursor(event:MouseEvent):void
    {
        cursor.x 
    = event.stageX;
        cursor.y 
    = event.stageY;
    }

      上面的代码运行时,如果你鼠标移动的比较慢的话,你会发现trace函数总是不能执行,这是为什么呢?

      原因是因为AS3事件的冒泡机制,事件的触发总是从发生事件的最子显示对象(child)开始向它的父显示对象(parent)逐次传递直到stage对象,这样将会发生两种情况:

    1. 如果两个显示对象是某个显示对象的子显示对象或孙显示对象或曾孙显示对象...但它们没有父子这样的容器(contains)关系(记住AS3中的父(parent)与子(child)并不是继承关系,而是包含关系(contains)),那么他们两个在某一时刻只有一个能捕获事件。

    2.如果某个显示对象A的索引(index,AS1和2中叫深度)比另一个显示对象B大,那么即便B与A处在同样的位置(即它们重叠),像鼠标事件发生在A上了,B就不会发生了,因为鼠标事件首先被A捕获到。

      正因为第2种情况,上面的第2段代码鼠标移动的比较慢时trace函数总是不能执行,因为mc显示对象首先添加,它的Index比cursor小。至于快速移动就能执行trace函数是因为cursor显示对象移动迟缓,跟不上鼠标的移动速度,导致鼠标在短时间里可以停留在mc上。

  • 相关阅读:
    【科创人独家】科界CTO林镇南:言必真,行必果,没有尽力而为,只有全力以赴
    【科创人+极客邦科技】百企贡献资源,携手抗击疫情
    【科创人新春篇】创投圈人脉王吴世春:肺炎,Long China,精益创业,回归听云…
    SAP QM 在Quality Notification里维护Internal Note
    地摊重现江湖,疫情带给我们的意外收获
    SAP QM Quality Notification的凭证流
    SAP QM 如何将一个附件挂在一个Quality Notification单据上?
    K项目轶事之被客户通报批评
    SAP MM VL34事务代码批量创建Inbound Delivery
    城市的房价稳定,就能不断吸引人才?
  • 原文地址:https://www.cnblogs.com/pains/p/1690783.html
Copyright © 2020-2023  润新知