【问题标题】:Actionscript 3 and dynamic masksActionscript 3 和动态掩码
【发布时间】:2010-11-08 13:27:32
【问题描述】:

我有一个容器 MovieClip,它用作我需要屏蔽的内容区域。当我使用 Shape 在此容器内创建蒙版时,我似乎无法与我在此处创建的其他容器的内容进行交互,例如按钮等。

这就是我在代码中所做的(我省略了所有导入等):

class MyContainer extends MovieClip
{
   protected var masker : Shape = null;
   protected var panel : MyPanel = null;

   public function MyContainer()
   {
      this.masker = new Shape();
      this.masker.graphics.clear();
      this.masker.graphics.beginFill( 0x00ff00 );
      this.masker.graphics.drawRect(0, 0, 1, 1);  // 1x1 pixel.
      this.masker.graphics.endFill();
      addChild( this.masker );

      this.panel = new MyPanel();  // has buttons and stuff.
      addChild( this.panel );

      this.mask = this.masker;
   }

   // called by it's parent when the stage is resized.
   public function resize( width : Number, height : Number ) : void
   {
      // set the mask to half the size of the stage.
      this.masker.width  = width  / 2;
      this.masker.height = height / 2;

      // set the panel to half the size of the stage.
      this.panel.resize( width / 2, height / 2);
   }
}

当我将遮罩(形状)添加到显示层次结构时,我无法再与 MyPanel 中定义的任何按钮进行交互。但是,not 将遮罩添加到显示层次结构确实让我可以与 MyPanel 上的内容进行交互,但遮罩的大小/位置不正确。我猜当掩码未添加到显示层次结构中时,它位于电影的左上角(但我无法证明这一点)。

如何正确设置我的蒙版大小/位置并让用户与 MyPanel 中的按钮进行交互?

【问题讨论】:

    标签: actionscript-3 actionscript dynamic mask


    【解决方案1】:
    this.masker.mouseEnabled = false; //set on this.masker
    

    应该可以。现在,它会在您的事件到达容器中的任何其他内容之前拦截它们。不是 100% 肯定,但我相信口罩会放在容器顶部。另一种选择是在 displayList 的底部向 MyContainer 添加另一个屏蔽子项,并将面板添加为其兄弟。不过,您仍需要在蒙面精灵/mc 上将 mouseEnabled 和 mouseChildren 设置为 false。

    package
    {
        import flash.display.Shape;
        import flash.display.Sprite;
    
        public class MyContainer extends Sprite
        {
           protected var masker : Shape = null;
           protected var panel:MyPanel;
    
           public function MyContainer()
           {
    
              this.masker = new Shape();
              this.masker.graphics.clear();
              this.masker.graphics.beginFill( 0x00ff00 );
              this.masker.graphics.drawRect(0, 0, 1, 1);  // 1x1 pixel.
              this.masker.graphics.endFill();
              addChild( this.masker );
    
              this.panel = new MyPanel();
              this.addChild(panel);
              this.mask = this.masker;
           }
    
           // called by it's parent when the stage is resized.
           public function resize( width : Number, height : Number ) : void
           {
              // set the mask to half the size of the stage.
              this.masker.width  = width  / 2;
              this.masker.height = height / 2;
    
              // set the panel to half the size of the stage.
           }
        }
    }
    

    -

    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
    
        public class MyPanel extends Sprite
        {
            public function MyPanel()
            {
                this.graphics.beginFill(0xFF0000)
                this.graphics.drawRect(0,0,10,10);
                this.addEventListener(MouseEvent.MOUSE_OVER, this.handleMouseOver)
                this.addEventListener(MouseEvent.MOUSE_OUT, this.handleMouseOut)
            }
    
            public function handleMouseOver(event:Event):void
            {
                this.graphics.clear();
                this.graphics.beginFill(0x00FF00)
                this.graphics.drawRect(0,0,10,10);
            }
    
            public function handleMouseOut(event:Event):void
            {
                this.graphics.clear();
                this.graphics.beginFill(0xFF0000)
                this.graphics.drawRect(0,0,10,10);
            }
        }
    }
    

    【讨论】:

    • 但是 masker 是一个 Shape,因此没有 mouseEnabled 属性。这是我使用 Shape 而不是 Sprite 的原因。
    • 刚刚用 Sprite 而不是 Shape 测试了它(mouseEnabled 属性设置为 false)。也不行。 :(
    • 也许问题出在你的 MyPanel 类上?我用上面添加的代码对其进行了测试,它按预期工作。
    • MyPanel 类只是容器的内容,它可以是任何东西。我想说的是,通过将遮罩添加到显示层次结构中,您无法与该遮罩显示对象范围内的任何对象进行交互。
    • 上面的代码可以让你按预期与MyPanel进行交互。
    【解决方案2】:

    我注意到的一件事是,如果蒙面对象的子项中的蒙版,则所有与鼠标相关的事件或交互的蒙版位置都将关闭,而视觉上一切似乎都很好。

    例如,我有一个剪辑,里面有一个按钮,它被其中一个孩子遮住了。发生的事情是只有一半的按钮可以被点击,如果我将剪辑向左移动一点,只有四分之一的按钮仍然可以点击。

    基本上,鼠标事件屏蔽似乎相对于被屏蔽对象的父对象定位自身,而视觉屏蔽则没有。

    您需要将蒙版移动到父剪辑。我是怎么做到的:

    class MaskedObject extends MovieClip{
      public var maskClip:MovieClip;
      public var maskOrigin:Point
    
      public function MaskedObject (){
         maskOrigin = new Point(maskClip.x,maskClip.y);
         parent.addChild(maskClip);
         maskClip.x = maskOrigin.x + this.x;
         maskClip.y = maskOrigin.y + this.y;
         mask = maskClip;
      }
    
      override function set x(val:Number):void{
        super.x = val;
        maskClip.x = maskOrigin.x + this.x;
      }
    
    
      override function set y(val:Number):void{
        super.y = val;
        maskClip.y = maskOrigin.y + this.y;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多