2009-07-07 9 views
3

私はマスクする必要のあるコンテンツ領域として機能するコンテナMovieClipを持っています。私は形を使用して、このコンテナ内のマスクを作成するとき、私はボタンなどのように、私はここで作成した他のコンテナの内容と対話するように見えることはできませんActionscript 3と動的マスク

これは私が(私が残してきたコードでやっているものですすべてのインポートのなどアウト):

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のボタンと対話してみましょうか?

答えて

5
this.masker.mouseEnabled = false; //set on this.masker 

これでうまくいくはずです。今はコンテナ内の他のものに行く前にイベントを傍受しています。 100%は確かではありませんが、私はマスクが容器の上に座ると信じています。もう1つのオプションは、別のマスキング子を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); 
     } 
    } 
} 
+0

しかし、マスカーはシェイプであり、したがってmouseEnabledプロパティがありません。スプライトの代わりにシェイプを使用した理由です。 – Luke

+0

Shapeの代わりにSpriteを使ってテストしました(mouseEnabledプロパティをfalseに設定)。どちらもうまくいきません。 : – Luke

+0

おそらく問題はあなたのMyPanelクラスにありますか?上記の追加されたコードでテストしたところ、期待どおりに動作します –

1

マスクされたオブジェクトの子ではマスクは、すべてのマウス関連のイベントや対話のために、マスクの位置を視覚的にすべてのものながら、オフになる場合、私は気づいたことの一つは、うまくいくようです。例えば

私はそれはそれの子のいずれかによってマスクされた最後の内側のボタンでクリップを持っていました。何が起こったのかは、ボタンの半分しかクリックできなかったことです。クリップを少し左に動かすと、ボタンの4分の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; 
    } 
} 
関連する問題