3

我有一个容器 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 中定义的任何按钮进行交互。但是,将遮罩添加到显示层次结构确实让我可以与 MyPanel 上的内容进行交互,但遮罩的大小/位置不正确。我猜想当掩码未添加到显示层次结构中时,它位于电影的左上角(虽然我无法证明这一点)。

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

4

2 回答 2

5
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);
        }
    }
}
于 2009-07-07T01:37:05.923 回答
1

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

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

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

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

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;
  }
}
于 2011-12-09T14:32:46.233 回答