2011-08-13 52 views
0

我有一個在父級(非顯示對象)類中組成的MovieClip。我們在該movieclip上註冊了一個事件偵聽器 - 例如一個CLICK處理程序。在事件後獲取組合的MovieClip的包含(父級)類

With event.target我可以從事件處理函數中獲取對MovieClip的引用。但是我怎樣才能把它引用到它的作文課堂上呢?

我可以簡單地在動態MovieClip類中添加一個「parentClass」屬性,但我想知道是否有更優雅/習慣的方式來做這件事,我應該考慮?

回答

1

如果創建您的MovieClip的類不是顯示對象,那麼它不能被視爲其父項。父元素將是您的MovieClip所附加的元素。創建類包含的所有內容都是對對象的引用,就像您在其他地方引用MovieClip一樣。

我的首選方法是創建一個MovieClip的後代類,它可以包含對創建類的引用,然後您將使用該引用而不是MovieClip

package { 
    import flash.display.MovieClip; 

    public class MovieClipWithRef extends MovieClip 
    { 
     private var _parentRef:Object; //obviously cast this as accurately as possible 

     public function MovieClipWithRef($ref:Object):void 
     { 
      _parentRef = $ref; 
     } 

     public function get parentRef():Object 
     { 
      return _parentRef; 
     } 
     //no setter makes this property read-only once set by the constructor 
    } 
} 
+0

謝謝!現在如果MC是一個已經在「舞臺」上的設計時間剪輯呢?這會改變什麼嗎? –

+0

如果它只是舞臺上的MovieClip,那麼您可以使用'parent'來查找它的第一個顯示樹祖先。你是這個意思嗎? – shanethehat

+0

不,我的意思是,我仍然在非顯示對象類中編寫它,但只在ADDED_TO_STAGE之後的舞臺上找到它。我想你只是將它作爲MovieClipWithRef投入並繼續你的快樂方式? –

1

這個答案基本上和shanethehat的答案一樣,但也包含了複合設計模式。請看下面的例子:

ICompositeObject.as:

package com.flashdevelopprojects.patterns.composite 
{ 
    public interface ICompositeObject 
    { 
     function get parentObject():Object 

    }// end interface 

}// end package 

CompositeObject.as:

package com.flashdevelopprojects.patterns.composite 
{ 
    public class CompositeObject implements ICompositeObject 
    { 
     private var _parentObject:Object; 

     public function get parentObject():Object { return _parentObject } 

     public function CompositeObject(parentObject:Object) 
     { 
      _parentObject = parentObject; 

     }// end function 

    }// end class 

}// end package 

Main.as(文檔類):

package 
{ 
    import com.flashdevelopprojects.patterns.composite.CompositeObject; 
    import com.flashdevelopprojects.patterns.composite.ICompositeObject; 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var a:A = new A(); 
      a.b.addEventListener(Event.ADDED_TO_STAGE, onBAddedToStage); 
      addChild(a.b); 

     }// end function 

     public function onBAddedToStage(e:Event):void 
     { 
      var b:ICompositeObject = ICompositeObject(e.target); 

      trace(b.parentObject); // output: A 

     }// end function 


    }// end class 

}// end package 

import com.flashdevelopprojects.patterns.composite.CompositeObject; 
import com.flashdevelopprojects.patterns.composite.ICompositeObject; 
import flash.display.MovieClip; 

internal class A 
{ 
    private var _b:B; 

    public function get b():B { return _b } 

    public function A() 
    { 
     _b = new B(this); 

    }// end function 

    public function toString():String { return "A" } 

}// end class 

internal class B extends MovieClip implements ICompositeObject 
{ 
    private var _compositeObject:CompositeObject; 

    public function get parentObject():Object { return _compositeObject.parentObject } 

    public function B(parentObject:Object) 
    { 
     _compositeObject = new CompositeObject(parentObject); 

    }// end function 

}// end class 
+0

+1使用複合DP。 –