2011-12-23 39 views
0

我想要在movieclip(ChildClass)的視覺父級中獲取e.target的值。請參閱* ** * ** * ** *在下面的代碼。如何捕捉小孩的event.target?

public class ParentClass extends MovieClip 
{ 

    public function ParentClass() 
    { 
    addEventListener(ChildClass.SOMETHING_HAPPENED, somethingHappened) 
    } 

    public function somethingHappened(e:Event) 
    { 
     //************************************************** 
     //************************************************** 
     // WANT TO GET THE VALUE OF e.target (ie. instance of SimpleButton) OF CHILD CLASS 
     //**************************************************** 
     //************************************************** 
    } 


} 


    public class ChildClass extends MovieClip 
{ 
     public static const SOMETHING_HAPPENED:String = "someThingHappened" ; 

    public function ChildClass() 
    { 
    my_btn.addEventListener(MouseEvent.CLICK, my_btn_CLICK) 
    } 

    public function my_btn_CLICK(e:Event) 
    { 
     dispatchEvent(new Event(ChildClass.SOMETHING_HAPPENED)); 
    } 


} 

回答

1

請考慮從顯示列表上的對象派發的事件將通過其上方的每個容器。假設您的顯示列表看起來像

stage 
> parent 
> > child 
> > > button 

如果button調度一個事件,那麼你可以聽它stageparentchildbutton!正因爲如此,您可以讓button派發事件,但在父項上監聽它。

在你的子類:現在

private function my_btn_CLICK(e:Event):void 
{ 
    button.dispatchEvent(new Event(SOMETHING_HAPPENED)); 
} 

,在你的父類:

public function somethingHappened(event:Event):void 
{ 
    trace(event.target);  // The target will be the button. 
    trace(event.currentTarget); // The current target will be this object. 
}