【问题标题】:mouseChildren = false not working very well for memouseChildren = false 对我来说效果不佳
【发布时间】:2012-02-01 03:45:20
【问题描述】:

顺便说一句,新年快乐!

我想将事件处理与容器和子容器分开。如您所见,我的源代码非常简单:

    package  {
    import flash.display.Sprite;
    import flash.display.*;
    import flash.events.*;

    public class test extends Sprite{

        public function test() {
            var container:Sprite = new Sprite();  // my container
            container.graphics.beginFill(0, 1);  // whatever the color
            container.graphics.drawRect(0, 0, 100, 100); // origin at 0,0
            container.graphics.endFill();
            addChild(container);

            var decor:Sprite = new Sprite();  // and it child
            decor.graphics.beginFill(0, 1);  // whatever the color
            decor.graphics.drawRect(200, 200, 100, 100);  // origin at 200,200
            decor.graphics.endFill();
            container.addChild(decor);
            container.mouseChildren = false;
            container.addEventListener(MouseEvent.ROLL_OVER, onOver, false, 0, true);
        }
        private function onOver(e: MouseEvent):void {
            trace("ROLL trace");
        }
    }
}

当我滚动容器对象时,我得到了跟踪(对我来说没问题)。 但是当我翻转装饰对象时,我也有痕迹(不是我想要的)。 我只希望容器由鼠标事件触发,而不是子事件。 那么我的 mouseChildren = false 怎么了……?没看懂……

【问题讨论】:

    标签: flash addeventlistener addchild


    【解决方案1】:

    decor 对象是container 的成员,因此它与container 中的任何其他内容一起被评估。

    mouseChildren = false; 不是完全禁用鼠标事件的方法,而是降低复合显示对象的复杂性:鼠标事件仍会被触发,但事件的 target 属性将不包含对鼠标子对象的引用实际上翻转了,但仅限于设置属性的父级。

    如果您希望完全忽略 decor,请改用 decor.mouseEnabled = false;

    【讨论】:

    【解决方案2】:

    我试过mouseEnabled = false,它也不起作用。 在另一个论坛上,一个人告诉我'a filled object within container will trigger the event handler'。 所以他的解决方案是拥有容器,并创建 2 个孩子:一个处理鼠标事件,另一个作为装饰。

    而且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 2012-08-25
      • 2011-12-27
      • 2015-01-21
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多