2012-01-02 56 views
4

新年快樂btw!mouseChildren = false對我來說效果不是很好

我想從容器和它的孩子分開事件處理。所以你可以看到,我的源代碼是非常簡單的:

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"); 
     } 
    } 
} 

當我翻身的容器對象,我已經得到了跟蹤(OK對我來說)。 但是當我翻轉裝飾物時,我也得到了痕跡(不是我想要的)。 我只是想容器被鼠標事件觸發,而不是它的孩子。 那麼,我的mouseChildren = false ....發生了什麼?我不明白...

回答

4

decor對象是container的成員,因此它將與container中的任何其他內容一起評估。

mouseChildren = false;不完全禁用鼠標事件,而是要綜合顯示對象中降低複雜性的方式:鼠標事件仍在解僱,但該事件的target財產將不包含在子對象的引用鼠標實際上是滾到了,但只限於該財產所在的父母。

如果您希望decor被完全忽略,請改爲使用decor.mouseEnabled = false;

+0

+1對。只需從語言參考http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#mouseChildren獲取一些有關@nouatzi的免費信息。總是檢查AS3文檔!他們充滿了很棒的nfo。 – 2012-01-03 01:46:20

相關問題